[Mostly Solved] Propagation of custom QEvent?



  • I am creating custom QEvents (subclassed QInputEvent) in a thread other than the main thread. These events are then sent via "QCoreApplication::sendEvent":http://qt-project.org/doc/qt-5/qcoreapplication.html#sendEvent and received by an object in the main thread.

    The problem is that this kind of event will not be propagated to parents of said object, even if the object itself can't/won't handle them.

    What do I have to do to get propagating events?



  • I finally found "this":http://stackoverflow.com/questions/3180506/propagate-custom-qevent-to-parent-widget-in-qt-pyqt .

    So the "solution" to the problem is to reimplement QApplication::notify() (respectively QGuiApplication::notify() or QCoreApplication::notify(), depending on your needs).

    Here my code:
    @class CustomApplication : public QApplication
    {
    public:
    CustomApplication(int &argc, char **argv) : QApplication(argc, argv) {}

    virtual bool notify(QObject *object, QEvent *event)
    {
        if(event->type() >= QEvent::User)
        {
            bool res;
            if(!(res = qApp->eventFilter(object, event)) && (res = !object->eventFilter(object, event)))//send to application and object event filters
            {
                QWidget* w = object->isWidgetType() ? static_cast<QWidget *>(object) : 0;
                event->setAccepted(false);
                while(w)
                {
                    res = QApplication::notify(w, event);
                    //res = w->event(event);//this sadly doesn't work, since QWidget::event() is protected
                    if((res && event->isAccepted()) || w->isWindow())//don't propagate between windows
                    {
                        break;
                    }
                    w = w->parentWidget();
                }
            }
            return(res);
        }
        else
        {
            return(QApplication::notify(object, event));
        }
    }
    

    };@

    Even though my immediate problem is solved, a couple of questions remain:
    In line 17 I use the base classes notify (which never propagates custom events, which is the reason for this thread) but I would much prefer to go the "direct" route via QWidget::event(). It feels a bit wasted this way, especially since I am not sure about the return value I receive.

    There are widgets in my application that claim to have processed my custom event (they return true from their ::event() method). Among them QMainWindow and QStackedWidget. But since I have not reimplemented their ::event() methods, how and why can they return "true" while other widgets properly say they didn't recognize/need the event ("false")?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.