Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Issues with eventFilter
QtWS25 Last Chance

Issues with eventFilter

Scheduled Pinned Locked Moved Mobile and Embedded
21 Posts 3 Posters 11.2k 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.
  • M Offline
    M Offline
    McLion
    wrote on 10 Jan 2014, 10:08 last edited by
    #1

    Hi

    Since I have added an eventFilter on the Application object to catch all mouse/touch events to filter them I am having some issues . Basically, the eventFilter works. For debug purpose I just show the event and then forward it.

    However, with the eventFilter in place the system feels kind of irresponsive on the mouse/touch. What bugs even more is that the some functions seem not to fire always and have to be submitted multiple times until they really fire.
    i.e. something like that has sometimes to be called multiple times until the ui really changes:
    @
    ui->webGUI->hide();
    ui->WebBrowser->setGeometry(0, 0, 1280, 800);
    ui->WebBrowser->show();
    @

    If I remove the eventFilter, it's back to work as supposed.

    Any hint what I missed?
    Thanks
    McL

    b.t.w.: I am on Qt 4.6.3 on eLinux

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Jan 2014, 10:17 last edited by
      #2

      Hi,

      Printing e.g. using qDebug is an expensive operation so printing something for every event of the application will give you a big performance hit

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        McLion
        wrote on 10 Jan 2014, 10:31 last edited by
        #3

        Thanks for trying to help.
        Printing is only temporary to see what happens. Even if disabled, I have the issue that the code like in the sample above is not fired always.

        The code segment above is started by an external serial command. The external command is received and the code segment is called - I set a response on the serial port to check that. There is no debug printing involved here except for if the code really executes I do get 1 MouseMove event which I assume is normally due to the ui switch.

        My eventFilter currently does forward all events and looks like below. In the end, I need just to catch all mouse/touch events and filter(discard) depending on the active ui.
        @
        if (event->type() >= QEvent::MouseButtonPress &&
        event->type() <= QEvent::MouseMove )
        {
        qDebug("Touch/Mouse event type: %d", event->type());
        //return true;
        return QObject::eventFilter(obj, event);
        }
        else if (event->type() == QEvent::KeyPress)
        {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        qDebug("Key %d pressed, Event type: %d", keyEvent->key(), event->type());
        //return true;
        return QObject::eventFilter(obj, event);
        }
        else
        {
        return QObject::eventFilter(obj, event); // fwd to std event processing
        }

        @

        So, there must be something else that creates this issue.
        Any further suggestions are highly appreciated.
        Thanks.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 10 Jan 2014, 10:44 last edited by
          #4

          when checking for an enum value it's recommended to use a switch-case statement rather than an if-else-constrcut. Switch-case statements are much faster since the compiler can optimize with a lookup-table/hash-list.

          This (without qDebug prints) probably won't solve your issue, but worth a shot:
          @
          bool eventFilter(QObject* watched, QEvent* event)
          {
          switch( event->type() )
          {
          case QEvent::MouseButtonPress:
          case QEvent::MouseMove:
          case QEvent::MouseButtonRelease:
          //...
          break;
          case QEvent::KeyPress:
          //...
          break;
          }

          return false;
          }
          @

          Is the code you've posted really the full code of your eventFilter method?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            McLion
            wrote on 10 Jan 2014, 11:01 last edited by
            #5

            Thanks for the hint. I changed my code accordingly, and yes it's all. I just started to implement the event filter and "real" functionality is not yet in.
            @
            bool QTGUI_MainWindow::eventFilter(QObject *obj, QEvent *event)
            {
            switch( event->type() )
            {
            case QEvent::MouseButtonPress:
            case QEvent::MouseButtonRelease:
            case QEvent::MouseMove:
            case QEvent::MouseButtonDblClick:
            {
            qDebug("Touch/Mouse event type: %d", event->type());
            //return true;
            return QObject::eventFilter(obj, event);
            break;
            }
            case QEvent::KeyPress:
            {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            qDebug("Key %d pressed, Event type: %d", keyEvent->key(), event->type());
            //return true;
            return QObject::eventFilter(obj, event);
            break;
            }
            default:
            return QObject::eventFilter(obj, event); // fwd to std event processing
            }
            return false;
            }
            @

            1 Reply Last reply
            0
            • R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 10 Jan 2014, 11:14 last edited by
              #6

              i don't see how this no-op eventFilter should slow down your application...
              The issue must be somewhere else in your code.

              Another hint:
              you don't need to call QObject::eventFilter() in every case branch.
              Returning false once at the end of the eventFilter has the same effect ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • M Offline
                M Offline
                McLion
                wrote on 10 Jan 2014, 12:19 last edited by
                #7

                OK, thanks :)
                As far as I know the rest is ok as well:
                @
                class QTGUI_MainWindow : public QMainWindow
                {
                Q_OBJECT

                protected:
                bool eventFilter(QObject *obj, QEvent *event);
                ...
                @

                @
                int main(int argc, char *argv[])
                {
                QApplication program(argc, argv);
                ...
                QTGUI_MainWindow eventFilter;
                program.installEventFilter(&eventFilter);
                ...
                @

                I'm out of ideas ...

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  McLion
                  wrote on 10 Jan 2014, 12:46 last edited by
                  #8

                  Since the eventFilter on the QApplication objects generally slows down the event handling (according doc), is there another way to catch all mouse/touch events?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 10 Jan 2014, 12:50 last edited by
                    #9

                    yes, you can subclass QApplication and reimplement "notify()":http://qt-project.org/doc/qt-4.8/qapplication.html#notify.
                    This has the advantage that this gets only called once for each event and not multiple times like the event filter would do since the events will get propagated...

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      McLion
                      wrote on 10 Jan 2014, 12:57 last edited by
                      #10

                      I did not really understand everything you wrote. Probably because I am not (yet) very experienced with Qt nor with C++ - I am a "C" guy.
                      Sure I can dig up some more on that ... thanks anyway :)

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        raven-worx
                        Moderators
                        wrote on 10 Jan 2014, 13:11 last edited by
                        #11

                        Code is a language every programmer understands... so here you go ;)

                        @
                        class MyApplication : public QApplication
                        {
                        public:
                        MyApplication(...) : QApplication(...)
                        {
                        }

                        virtual bool notify ( QObject * receiver, QEvent * event )
                        {
                                //same code like in your previous eventFilter() implementation
                                if( event-should-be-filtered-out )
                                      return true;
                        
                                 QApplication::notify(receiver,event);
                        }
                        

                        };

                        int main(int argc, char *argv[])
                        {
                        MyApplication program(argc, argv);
                        ...
                        QTGUI_MainWindow mainWin;

                        ...
                        }
                        @

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          McLion
                          wrote on 10 Jan 2014, 14:54 last edited by
                          #12

                          Thanks a million!
                          It works ... and solves my issue!
                          Responsiveness is back and everything works as supposed.

                          Best regards (from aprox. 500 km west)

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            McLion
                            wrote on 14 Jan 2014, 09:02 last edited by
                            #13

                            I am extending it ... and got stuck.
                            I seem not to be able to call a function that is defined in another class from notify().
                            What is the correct way to do this?

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              raven-worx
                              Moderators
                              wrote on 14 Jan 2014, 09:05 last edited by
                              #14

                              [quote author="McLion" date="1389690173"]
                              I seem not to be able to call a function that is defined in another class from notify().[/quote]
                              what exactly is preventing you from calling a method of another class?!

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                McLion
                                wrote on 14 Jan 2014, 09:10 last edited by
                                #15

                                Getting:
                                @./release\moc_qtgui_mainwindow.o:moc_qtgui_mainwindow.cpp:(.text$_ZN13MyApplication6notifyEP7QObjectP6QEvent[MyApplication::notify(QObject*, QEvent*)]+0x76): undefined reference to `writeToTCOport(QByteArray)'
                                collect2: ld returned 1 exit status@

                                The other class:
                                @class QTGUI_MainWindow : public QMainWindow
                                {
                                Q_OBJECT

                                public:
                                explicit QTGUI_MainWindow(QWidget *parent = 0);
                                ~QTGUI_MainWindow();
                                void writeToTCOport(QByteArray dataBA);
                                ....@

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  raven-worx
                                  Moderators
                                  wrote on 14 Jan 2014, 09:17 last edited by
                                  #16

                                  in that order:

                                  clean your project

                                  rerun qmake

                                  rebuild project

                                  or more likely i assume your are just calling the method without an object (your mainwindow instance) on or connecting a signal to it, right? (post the code of the call to writeToTCOport() inside the notify() implementation).

                                  if the error remains:
                                  Are the implementations of the QTGUI-MainWindow and MyApplication classes in the same project? Or e.g. loaded in a library?

                                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                  If you have a question please use the forum so others can benefit from the solution in the future

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    McLion
                                    wrote on 14 Jan 2014, 09:36 last edited by
                                    #17

                                    Cleaning and rebuilding did not help.

                                    [quote author="raven-worx" date="1389691039"]
                                    ... or more likely i assume your are just calling the method without an object (your mainwindow instance) on or connecting a signal to it, right? (post the code of the call to writeToTCOport() inside the notify() implementation).
                                    [/quote]

                                    I get:
                                    @..\src/qtgui_mainwindow.h: In member function 'virtual bool MyApplication::notify(QObject*, QEvent*)':
                                    ..\src/qtgui_mainwindow.h:86: error: cannot call member function 'void QTGUI_MainWindow::writeToTCOport(QByteArray)' without object@
                                    when I call it like:
                                    @QTGUI_MainWindow::writeToTCOport(sendData);@

                                    And calling without QTGUI_MainWindow:: returns the error posted before.
                                    I seem to miss some of the basic c ++ concepts ..

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      raven-worx
                                      Moderators
                                      wrote on 14 Jan 2014, 09:43 last edited by
                                      #18

                                      @error: cannot call member function 'void QTGUI_MainWindow::writeToTCOport(QByteArray)' without object@
                                      says it clearly: you need an object/instance to call it on.

                                      The call
                                      @QTGUI_MainWindow::writeToTCOport(sendData);@
                                      would be fine when you define the method "static". You can define a method static if you don't need to access object specific members within it. If that's ok in this method do it.

                                      Otherwise i would suggest something like this for example:
                                      @
                                      class MyApplication : public QApplication
                                      {
                                      public:
                                      MyApplication(...) : QApplication(...)
                                      {
                                      }

                                          virtual bool notify ( QObject * receiver, QEvent * event )
                                          {
                                                  //same code like in your previous eventFilter() implementation
                                                  if( event-should-be-filtered-out )
                                                        return true;
                                       
                                                   if( m_MainWindow )
                                                        m_MainWindow->writeToTCOport(...);
                                      
                                                   QApplication::notify(receiver,event);
                                          }
                                      
                                          void setMainWindowObject(QTGUI_MainWindow* mainWin)
                                          {
                                                  m_MainWindow = mainWin;
                                          }
                                      
                                      protected:
                                                QPointer<QTGUI_MainWindow> m_MainWindow;
                                      };
                                       
                                       
                                      int main(int argc, char *argv[])
                                      {
                                          MyApplication program(argc, argv);
                                      ...
                                          QTGUI_MainWindow mainWin;
                                          program.setMainWindowObject(&mainWin);
                                      ...
                                      }
                                      

                                      @

                                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                      If you have a question please use the forum so others can benefit from the solution in the future

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        McLion
                                        wrote on 14 Jan 2014, 10:57 last edited by
                                        #19

                                        Thanks - tried the static solution. Compiles as long as the function is empty. As soon as I add port->write(); like in many other places I get back the same error.
                                        Going to read your second suggestion ... as well as some C++ tutorials ;)

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          raven-worx
                                          Moderators
                                          wrote on 14 Jan 2014, 11:01 last edited by
                                          #20

                                          as i said:
                                          [quote author="raven-worx" date="1389692609"]You can define a method static if you don't need to access object specific members within it.[/quote]

                                          i guess "port" is a member of the class?

                                          [quote author="McLion" date="1389697075"]
                                          Going to read your second suggestion ... as well as some C++ tutorials ;)[/quote]

                                          yes, good idea. ;)

                                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                          If you have a question please use the forum so others can benefit from the solution in the future

                                          1 Reply Last reply
                                          0

                                          3/21

                                          10 Jan 2014, 10:31

                                          topic:navigator.unread, 18
                                          • Login

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