Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Mostly Solved] Propagation of custom QEvent?
QtWS25 Last Chance

[Mostly Solved] Propagation of custom QEvent?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thEClaw
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      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")?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved