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. eventFilter anywhere in the program.
Forum Updated to NodeBB v4.3 + New Features

eventFilter anywhere in the program.

Scheduled Pinned Locked Moved Solved General and Desktop
installeventfileventfilter
21 Posts 5 Posters 20.1k Views 2 Watching
  • 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.
  • mrjjM mrjj

    Hi
    Not sure its the best way but worked for my use case.
    I installed the eventfilter on the application and return false to let it
    give to any other event filter installed. ( like for comboboxes)

    IN ITS OWN FILE, NOT PART OF MAIN !
    (i inlined it to be easier to show)
    #include <QObject>
    #include <QMouseEvent>
    #include <QDebug>
    #include <QCursor>
    
    class myEventFilter: public QObject {
      Q_OBJECT
    public:
      myEventFilter() {}
      ~myEventFilter() {
      }
    protected:
      bool eventFilter(QObject* object, QEvent* event) {
        if(event->type() == QEvent::MouseMove) {
          int x = QCursor::pos().x();
          int y = QCursor::pos().y();
          qDebug() << "MP -> (" + QString::number(x) + "," + QString::number(y) + ")";
          return false; // make it unhandled and sent to other filters.
        } else
          return false;
      }
    };
    
    ---
    then in main.cpp
    
    #include "filterclass.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        a.installEventFilter(new myEventFilter()); // this can be controlled by a flag.
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    
    O Offline
    O Offline
    Oleg21
    wrote on last edited by
    #7

    @mrjj Hi, I wanted to ask: do you had the effect of double-running eventFilter?
    That is, I create a global variable and in eventFilter I increment it. I output the result in qDebug. But I see that the variable does not increase by one unit, but increases by two units.

    mrjjM 1 Reply Last reply
    0
    • O Oleg21

      @mrjj Hi, I wanted to ask: do you had the effect of double-running eventFilter?
      That is, I create a global variable and in eventFilter I increment it. I output the result in qDebug. But I see that the variable does not increase by one unit, but increases by two units.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #8

      @Oleg21

      Hmm. Only one time where i did install it multiple times.
      You just did like
      int cc=0;

      --

      and in eventfilter
      cc++; ?

      Note that doc says:
      "If multiple event filters are installed on a single object, the filter that was installed last is activated first."

      O 1 Reply Last reply
      0
      • mrjjM mrjj

        @Oleg21

        Hmm. Only one time where i did install it multiple times.
        You just did like
        int cc=0;

        --

        and in eventfilter
        cc++; ?

        Note that doc says:
        "If multiple event filters are installed on a single object, the filter that was installed last is activated first."

        O Offline
        O Offline
        Oleg21
        wrote on last edited by
        #9

        @mrjj So I found my mistake! I did not listen to you and now I have a negative result. Here's the problem:

        • You wrote that you should install event filter in application. As shown in code main.cpp:
        #include "first.h"
        #include <QApplication>
        #include "second.h"
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            first w;
            
            w.installEventFilter(new second());
            
            w.show();
        
            return a.exec();
        }
        
        • But, I decided I could install the event filter in the second.cpp class constructor:
        second::second(QWidget *parent) : QWidget(parent)
        {
            qApp->installEventFilter(this);
        }
        

        This was the cause of incorrect behavior. I'm sorry.

        But still, maybe you know why this is happening?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #10

          Hi
          You seems to install 2 filters.
          w.installEventFilter(new second());
          w is first class. not application. Not sure what that expression does as
          a constructor cannot return anything.

          qApp->installEventFilter(this); // one on app.

          if you want second to handle the filter for application, you can just do do
          w.installEventFilter(new second());
          second local;

          But its a bit odd as you dont use instance and its nothing more than just
          qApp->installEventFilter(this); // to get just one

          So what/why you want second class? if i may ask.
          You plan it offers other service than eventfilter?

          O 1 Reply Last reply
          1
          • mrjjM mrjj

            Hi
            You seems to install 2 filters.
            w.installEventFilter(new second());
            w is first class. not application. Not sure what that expression does as
            a constructor cannot return anything.

            qApp->installEventFilter(this); // one on app.

            if you want second to handle the filter for application, you can just do do
            w.installEventFilter(new second());
            second local;

            But its a bit odd as you dont use instance and its nothing more than just
            qApp->installEventFilter(this); // to get just one

            So what/why you want second class? if i may ask.
            You plan it offers other service than eventfilter?

            O Offline
            O Offline
            Oleg21
            wrote on last edited by
            #11

            @mrjj Hi, of course I will answer. All this interests me because I want to implement the code in a separate class. This separate class, I want to include to different projects, not thinking about implementation of the code.

            Simply put, all things related to eventFilter are placed in a separate class. And then include it where you need it without completing any more code.

            However, this is no longer important. By your example, I will be able to come up with something. Thanks again for the advice.

            mrjjM 1 Reply Last reply
            0
            • O Oleg21

              @mrjj Hi, of course I will answer. All this interests me because I want to implement the code in a separate class. This separate class, I want to include to different projects, not thinking about implementation of the code.

              Simply put, all things related to eventFilter are placed in a separate class. And then include it where you need it without completing any more code.

              However, this is no longer important. By your example, I will be able to come up with something. Thanks again for the advice.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @Oleg21
              Ok i understand. a utility class. That's not a bad idea.
              I have something the same where i included all events names so it can provide better info
              when using a filter. ( for spying ;)

              1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                Not sure its the best way but worked for my use case.
                I installed the eventfilter on the application and return false to let it
                give to any other event filter installed. ( like for comboboxes)

                IN ITS OWN FILE, NOT PART OF MAIN !
                (i inlined it to be easier to show)
                #include <QObject>
                #include <QMouseEvent>
                #include <QDebug>
                #include <QCursor>
                
                class myEventFilter: public QObject {
                  Q_OBJECT
                public:
                  myEventFilter() {}
                  ~myEventFilter() {
                  }
                protected:
                  bool eventFilter(QObject* object, QEvent* event) {
                    if(event->type() == QEvent::MouseMove) {
                      int x = QCursor::pos().x();
                      int y = QCursor::pos().y();
                      qDebug() << "MP -> (" + QString::number(x) + "," + QString::number(y) + ")";
                      return false; // make it unhandled and sent to other filters.
                    } else
                      return false;
                  }
                };
                
                ---
                then in main.cpp
                
                #include "filterclass.h"
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    a.installEventFilter(new myEventFilter()); // this can be controlled by a flag.
                    MainWindow w;
                    w.show();
                    return a.exec();
                }
                
                
                S Offline
                S Offline
                sogo
                wrote on last edited by sogo
                #13

                @mrjj @jsulm Hi, I know this is fairly old post but I am facing kind of similar problem so I thought of reviving it for some help. I am working on Image viewing app with support of adding comments to it. My MainWindow class consists of two Qlabel widgets to show images and and one QTextEdit widget to show and write comments. Now I am using eventFilter to scroll through list of images in my directory folder using keyboard arrow up and down key (working fine) and want to implement a Flagging system in comment box which detect keyboard shortcut keys (set by me) to add automatic flags (comments) in textEditor. I need to install another eventFilter for textEditor which will filter only my keyboard shortcuts as I don't want up and down arrow keys to be caught by my textEditor as they are used for navigation through text box. Imagine I press up arrow key through my comment box to access upper line, image changes on imageLabel.

                What I tried so far:

                1. I tried to create new class and getting textEdit widget from my MainWindow class but I came to know widgets can not be accessed from another class
                2. So I created a class named filter, created eventFilter function in that class header file, added filter.h file in my MainWindow class and installed that class to my textEdit in MainWindow but it is not working. I want to know is this method correct as I am not sure if I am doing okay

                I can share code if you understand my problem, Thanks
                p.s. I see this thread is solved so I don't know if it is okay to reply to it like this

                UPDATE:
                I am able to get my filter class in my MainWindow class by adding this line in my MainWindow.cpp constructor : "filter class = new filter class"
                But now I am unable to write anything my text editor but it can catch keyboard keys.

                jsulmJ 1 Reply Last reply
                0
                • S sogo

                  @mrjj @jsulm Hi, I know this is fairly old post but I am facing kind of similar problem so I thought of reviving it for some help. I am working on Image viewing app with support of adding comments to it. My MainWindow class consists of two Qlabel widgets to show images and and one QTextEdit widget to show and write comments. Now I am using eventFilter to scroll through list of images in my directory folder using keyboard arrow up and down key (working fine) and want to implement a Flagging system in comment box which detect keyboard shortcut keys (set by me) to add automatic flags (comments) in textEditor. I need to install another eventFilter for textEditor which will filter only my keyboard shortcuts as I don't want up and down arrow keys to be caught by my textEditor as they are used for navigation through text box. Imagine I press up arrow key through my comment box to access upper line, image changes on imageLabel.

                  What I tried so far:

                  1. I tried to create new class and getting textEdit widget from my MainWindow class but I came to know widgets can not be accessed from another class
                  2. So I created a class named filter, created eventFilter function in that class header file, added filter.h file in my MainWindow class and installed that class to my textEdit in MainWindow but it is not working. I want to know is this method correct as I am not sure if I am doing okay

                  I can share code if you understand my problem, Thanks
                  p.s. I see this thread is solved so I don't know if it is okay to reply to it like this

                  UPDATE:
                  I am able to get my filter class in my MainWindow class by adding this line in my MainWindow.cpp constructor : "filter class = new filter class"
                  But now I am unable to write anything my text editor but it can catch keyboard keys.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  @sogo said in eventFilter anywhere in the program.:

                  but I came to know widgets can not be accessed from another class

                  They can, but you should not do that.
                  You will need to post your code, else it is just guessing.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @sogo said in eventFilter anywhere in the program.:

                    but I came to know widgets can not be accessed from another class

                    They can, but you should not do that.
                    You will need to post your code, else it is just guessing.

                    S Offline
                    S Offline
                    sogo
                    wrote on last edited by
                    #15

                    @jsulm So here is my code, I will only post relevant parts as it is a bit big and messy

                    imageviewer.h (MainWindow)

                    class Filter;
                    class ImageViewer : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        ImageViewer(QWidget *parent = nullptr);
                        Filter *filter;
                    protected:
                        void closeEvent(QCloseEvent *event);
                    
                        bool eventFilter(QObject* obj, QEvent *event); // This is mainwindow eventFilter for imageLabels
                    private:
                    QTextEdit *textEdit;
                    ......... rest of code
                    };
                    #endif // IMAGEVIEWER_H
                    

                    imageviewer.cpp (MainWindow)

                    ImageViewer::ImageViewer(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::ImageViewer)
                    {
                        ui->setupUi(this);
                        filter = new Filter;
                        textEdit = ui->textEdit;
                        textEdit->installEventFilter(filter);
                    ..... rest of constructor
                    }
                    

                    filter.h

                    #ifndef FILTER_H
                    #define FILTER_H
                    
                    #include <QMainWindow>
                    #include<QtGui>
                    
                    namespace Ui {
                    class Filter;
                    }
                    
                    class Filter : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        explicit Filter(QWidget *parent = nullptr);
                        bool eventFilter( QObject * object, QEvent * eve );
                        ~Filter();
                    
                    private:
                    
                    };
                    
                    #endif // FILTER_H
                    

                    filter.cpp

                    #include "filter.h"
                    
                    Filter::Filter(QWidget *parent) :
                        QMainWindow(parent)
                    {
                    
                    }
                    
                    Filter::~Filter()
                    {
                        delete this;
                    }
                    
                    bool Filter::eventFilter( QObject * object, QEvent * eve ){
                        if (eve->type() == QEvent::KeyRelease)
                        {
                            QKeyEvent* keyEven = static_cast<QKeyEvent*>(eve);
                            //QKeyEvent *e = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
                            if (keyEven->key() == Qt::Key_F8)
                            {
                                  qDebug()<<"here";
                                  return true;
                                }
                    }
                        return false;
                    }
                    

                    Now textEdit can catch my filter class eventFilter but I am not able to write anything in textEditor as it does not show blinking cursor, but it is able to catch keyboard keys.

                    jsulmJ 1 Reply Last reply
                    0
                    • S sogo

                      @jsulm So here is my code, I will only post relevant parts as it is a bit big and messy

                      imageviewer.h (MainWindow)

                      class Filter;
                      class ImageViewer : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          ImageViewer(QWidget *parent = nullptr);
                          Filter *filter;
                      protected:
                          void closeEvent(QCloseEvent *event);
                      
                          bool eventFilter(QObject* obj, QEvent *event); // This is mainwindow eventFilter for imageLabels
                      private:
                      QTextEdit *textEdit;
                      ......... rest of code
                      };
                      #endif // IMAGEVIEWER_H
                      

                      imageviewer.cpp (MainWindow)

                      ImageViewer::ImageViewer(QWidget *parent)
                          : QMainWindow(parent)
                          , ui(new Ui::ImageViewer)
                      {
                          ui->setupUi(this);
                          filter = new Filter;
                          textEdit = ui->textEdit;
                          textEdit->installEventFilter(filter);
                      ..... rest of constructor
                      }
                      

                      filter.h

                      #ifndef FILTER_H
                      #define FILTER_H
                      
                      #include <QMainWindow>
                      #include<QtGui>
                      
                      namespace Ui {
                      class Filter;
                      }
                      
                      class Filter : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          explicit Filter(QWidget *parent = nullptr);
                          bool eventFilter( QObject * object, QEvent * eve );
                          ~Filter();
                      
                      private:
                      
                      };
                      
                      #endif // FILTER_H
                      

                      filter.cpp

                      #include "filter.h"
                      
                      Filter::Filter(QWidget *parent) :
                          QMainWindow(parent)
                      {
                      
                      }
                      
                      Filter::~Filter()
                      {
                          delete this;
                      }
                      
                      bool Filter::eventFilter( QObject * object, QEvent * eve ){
                          if (eve->type() == QEvent::KeyRelease)
                          {
                              QKeyEvent* keyEven = static_cast<QKeyEvent*>(eve);
                              //QKeyEvent *e = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
                              if (keyEven->key() == Qt::Key_F8)
                              {
                                    qDebug()<<"here";
                                    return true;
                                  }
                      }
                          return false;
                      }
                      

                      Now textEdit can catch my filter class eventFilter but I am not able to write anything in textEditor as it does not show blinking cursor, but it is able to catch keyboard keys.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      @sogo said in eventFilter anywhere in the program.:

                      but I am not able to write anything in textEditor as it does not show blinking cursor

                      That's because you do not pass the events to base class.
                      See example from documentation:

                      void MyCheckBox::mousePressEvent(QMouseEvent *event)
                      {
                          if (event->button() == Qt::LeftButton) {
                              // handle left mouse button here
                          } else {
                              // pass on other buttons to base class
                              QCheckBox::mousePressEvent(event); // THIS
                          }
                      }
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      6
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        Hi
                        You steal all keys :)

                        https://wiki.qt.io/How_to_catch_enter_key

                        bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
                        {
                            if (event->type()==QEvent::KeyPress) {
                                QKeyEvent* key = static_cast<QKeyEvent*>(event);
                                if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                                    //Enter or return was pressed
                                } else {
                                    return QObject::eventFilter(obj, event);
                                }
                                return true;
                            } else {
                                return QObject::eventFilter(obj, event);
                            }
                            return false;
                        }
                        

                        Also your filter class is a QMainWindow which not needed
                        class Filter : public QObject
                        is enough :)

                        S 1 Reply Last reply
                        4
                        • mrjjM mrjj

                          Hi
                          You steal all keys :)

                          https://wiki.qt.io/How_to_catch_enter_key

                          bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
                          {
                              if (event->type()==QEvent::KeyPress) {
                                  QKeyEvent* key = static_cast<QKeyEvent*>(event);
                                  if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                                      //Enter or return was pressed
                                  } else {
                                      return QObject::eventFilter(obj, event);
                                  }
                                  return true;
                              } else {
                                  return QObject::eventFilter(obj, event);
                              }
                              return false;
                          }
                          

                          Also your filter class is a QMainWindow which not needed
                          class Filter : public QObject
                          is enough :)

                          S Offline
                          S Offline
                          sogo
                          wrote on last edited by
                          #18

                          @mrjj said in eventFilter anywhere in the program.:

                          Hi
                          You steal all keys :)

                          Lol this made me laugh, I see what I am doing wrong. I have been reading documentation all day, searching and somehow I skipped this. Thanks @mrjj @jsulm for the help, it works fine now.

                          Also I changed filter from QMainWindow to QObject, it's long story, Initially I was trying to create two classes in one file so when I called filter pointer to my MainWindow class constructor, it gave me error of QObject and QMainWindow difference, so I set filter to QMainWindow also. But now I made separate files as former didn't worked as expected.

                          1 Reply Last reply
                          1
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #19

                            Hi,

                            Technically, your ImageViewer class could directly be the event filter which might also be simpler depending on what you want to achieve.

                            You would need something like: textEdit->installEventFilter(this); in your ImageViewer constructor.

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

                            S 1 Reply Last reply
                            1
                            • SGaistS SGaist

                              Hi,

                              Technically, your ImageViewer class could directly be the event filter which might also be simpler depending on what you want to achieve.

                              You would need something like: textEdit->installEventFilter(this); in your ImageViewer constructor.

                              S Offline
                              S Offline
                              sogo
                              wrote on last edited by
                              #20

                              @SGaist Hi SGaist, sorry I wasn't reading the post here anymore as my issue is solved but thank you for your response. I explained in my previous post that I can not use same eventFilter due to up and down arrow keys being filtered for my imageLabel which will obstruct me from using these keys on my text box as up and down arrows are used to navigate through text. I needed a separate eventFilter for text box only.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #21

                                @sogo No you don't. eventFilter has two parameters so you can use the object parameter to ensure you apply your filter only on the widgets you want to and not on the others.

                                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
                                2

                                • Login

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