我需要做一个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,根本就没有在层次结构中冒泡。
那么,所有的事情都会泡汤吗?如果不是,是哪一个,哪一个不,为什么?怎样才能使我的活动泡沫化呢?
发布于 2016-01-18 15:36:43
我认为最好的方法是对QApplication进行子类化,重写notify方法,并自己执行“气泡事件”。我非常肯定,鼠标和键盘事件通过这种方法“冒泡”,而其他事件则不会。
bool QCoreApplication::notify(QObject * receiver,QEvent * event) 将事件发送给接收方:接收方->事件(事件)。返回从接收方的事件处理程序返回的值。请注意,对于发送到任何线程中的任何对象的所有事件,都会调用此函数。 对于某些类型的事件(例如鼠标和键事件),如果接收方对事件不感兴趣(即,它返回false),则事件将被传播到接收方的父对象等顶级对象。
所以你可以通过你的意识来改变这种行为。
也是关于你的代码示例。我想这张支票
if( qApp!=nullptr )是无用的,因为你总是会有qApp的实例。
https://stackoverflow.com/questions/34854546
复制相似问题