How to get data out of reimplemented QApplication notify

  • Hi

    I have reimplemented QApplicaction notify.
    Everything works perfect. I now need to catch the mouse cursor position and the click events and have them in my main class. How do I get them out of the notifier of MyApplication to my QTGUI_MainWindow class and use them there in the function ?
    Calling TouchEvent as below does not work. I also tried to create a new QTGUI_MainWindow object and use this ... copiles but crashes .

    class MyApplication : public QApplication
        MyApplication(int &argc, char **argv ) : QApplication(argc, argv)
        virtual bool notify ( QObject *receiver, QEvent *event )
          switch( event->type() )
            case QEvent::MouseButtonPress:
            case QEvent::MouseButtonRelease:
            case QEvent::MouseMove:
            case QEvent::MouseButtonDblClick:
              { QMouseEvent *TEvent = static_cast<QMouseEvent *>(event);
                QPoint cursorPos = TEvent->globalPos();
                TouchEvent(event->type(), cursorPos.rx(), cursorPos.ry());
                qDebug("Touch/Mouse event type: %d at %d, %d", event->type(), cursorPos.rx(), cursorPos.ry());
              { QMouseEvent *TEvent = static_cast<QMouseEvent *>(event);
                QPoint cursorPos = TEvent->globalPos();
                //qDebug("TouchInActive at %d, %d", cursorPos.rx(), cursorPos.ry());
                return true;  // return with true filters the event
            case QEvent::KeyPress:
            case QEvent::KeyRelease:
            { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
              qDebug("Key %d pressed, Event type: %d", keyEvent->key(), event->type());
              break; // break does not filter and forwards the event to QApplication::notify()
          return false;

    I know, it's about basic c++ understanding (classes ... ).
    However, I tried everything and don't seem to able to get it.
    Any hints are highly appreciated.

  • Lifetime Qt Champion


    Why don't you just reimplement the related event handler in your main window ?

  • @SGaist

    I did that first, but I had some performance issues and this could be solved this way.

    I'll try the following (which works for a plain signal w/o parameters .. and should work with parameters as well). I am not sure if this is "best practice", but it works:

    • Add a signal to the reimplemented QApplication MyApplication class
    • In main: connect the signal to a slot of my MainWindow class

  • After I saw that bTouchActive variable there I would say you're trying to manage a touch surface. In this case, why don't you have a look at QGesture ? It might be better suited for the task instead of tracking the mouse position in real time. By the way, real time mouse tracking is very intensive and it might be the reason why your event handler reimplementation is creating performance issues.

Log in to reply