我需要制作一个 GUI 按钮,告诉它的父级(或父级的父级,甚至是父级的父级的父级......)QStackedLayout
应该显示不同的小部件。我创建了一个自定义 QEvent:
class SwitchScreenEventWidget : public QEvent {
public:
SwitchScreenEventWidget(QWidget* w) : SwitchScreenEvent(), widget(w) {
if(widget==nullptr)
throw "SwitchScreenEventWidget received null widget.";
}
virtual QWidget* getWidget() const {;return widget;}
private:
QWidget* const widget;
};
我这样调用它:
// Through debugger I checked that this is getting called properly
void GraphButton::buttonClicked()
{
if(qApp!=nullptr && parent()!=nullptr)
qApp->notify(parent(), new SwitchScreenEventWidget(getGraph()));
}
并像这样处理它:
bool ViewStack::eventFilter(QEvent* e)
{
if(e->type()>=QEvent::User) {
if(SwitchScreenEvent* event = dynamic_cast<SwitchScreenEvent*>(e)) {
// Show the given widget
}
return true;
}
return false;
}
我使用eventFilter
它然后注册到主应用程序小部件。但是该事件没有被捕获。在某处我读到一些QEvent
s 根本不会在层次结构中冒泡。
那么所有事件都会冒泡吗?如果不是,哪些会,哪些不会,为什么?以及如何正确地使我的活动冒泡?