Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

eventFilter anywhere in the program.



  • Hi everyone. The following situation arose: I have a project, it contains two classes (first.cpp and second.cpp).
    first.cpp is a constant and compulsory project class, and the second.cpp is an optional class and is connected as needed.
    So, in the second.cpp class, I have a desire to implement eventFilter at the program level. For this I write the following code:

    second.cpp

    #include "second.h"
    #include <QtWidgets>
    
    second::second(QWidget *parent) : QWidget(parent)
    {
        qApp->installEventFilter(parent);
    }
    
    second::~second(){
    
    }
    
    bool second::eventFilter(QObject *watched, QEvent *event){
        //it's not working!
    }
    

    Unfortunately, eventFilter in this class does not work.
    In order for everything to work, I need to implement eventFilter in class first.cpp:

    #include "first.h"
    #include "second.h"
    
    first::first(QWidget *parent) : QMainWindow(parent)
    {
        second *tempSecond = new second(this);
    }
    
    first::~first()
    {
    
    }
    
    bool first::eventFilter(QObject *watched, QEvent *event){
        //it's working!
    }
    

    However, this approach contradicts my opinion about the possibility of disabling the class second.cpp.
    Tell me, how to solve this problem?


  • Qt Champions 2019

    @Oleg21 This cannot work this way.
    Please read http://doc.qt.io/qt-5/qobject.html#installEventFilter
    The event filter you install in second is only for second instances!
    Furthermore the object you set as event filter has to implement eventFilter method else it is not a filter - that's why first has to implement eventFilter in your example. If you need an eventFilter at the top level then you need to install an event filter object in the top level widget.
    From design point of view it is bad if some subwidget installs a top level event filter.


  • Lifetime Qt Champion

    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();
    }
    
    


  • @jsulm Hi, thank you for your advice. I have already thought of abandoning my idea. That is, implement all code in the class first. However, mrjj, offered a very interesting solution for me, I have to try it.



  • @mrjj Hi, thank you for the idea, I did not guess about such implementation of the code. I will definitely try this.



  • @mrjj Hi, so really, your decision helped me. Thanks!



  • @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.


  • Lifetime Qt Champion

    @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."



  • @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?


  • Lifetime Qt Champion

    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?



  • @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.


  • Lifetime Qt Champion

    @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 ;)



  • @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.


  • Qt Champions 2019

    @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.



  • @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.


  • Qt Champions 2019

    @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
        }
    }
    

  • Lifetime Qt Champion

    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 :)



  • @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.


  • Lifetime Qt Champion

    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.