Results 1 to 18 of 18

Thread: eventFilter anywhere in the program.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2017
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

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

    Qt Code:
    1. second.cpp
    2. #include "second.h"
    3. #include <QtWidgets>
    4.  
    5. second::second(QWidget *parent) : QWidget(parent)
    6. {
    7. qApp->installEventFilter(parent);
    8. }
    9.  
    10. second::~second(){
    11.  
    12. }
    13.  
    14. bool second::eventFilter(QObject *watched, QEvent *event){
    15. //it's not working!
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. #include "first.h"
    2. #include "second.h"
    3.  
    4. first::first(QWidget *parent) : QMainWindow(parent)
    5. {
    6. second *tempSecond = new second(this);
    7. }
    8.  
    9. first::~first()
    10. {
    11.  
    12. }
    13.  
    14. bool first::eventFilter(QObject *watched, QEvent *event){
    15. //it's working!
    16. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: eventFilter anywhere in the program.

    Qt Code:
    1. //qApp->installEventFilter(parent);
    2. qApp->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2017
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: eventFilter anywhere in the program.

    Hello. Truly, I had to do as you say:
    Qt Code:
    1. qApp->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    And implementation of eventFilter:
    Qt Code:
    1. bool second::eventFilter(QObject *watched, QEvent *event){
    2. if (event->type() == QEvent::MouseButtonRelease) {
    3.  
    4. cc++;
    5. qDebug() << cc;
    6.  
    7. return false;
    8. }else{
    9. return false;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    However, there is a small problem here: eventFilter function gets MouseButtonRelease event twice.
    Should this be so? If so, maybe you know why this happens?
    Last edited by Oleg21; 12th October 2017 at 19:41.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: eventFilter anywhere in the program.

    Quote Originally Posted by Oleg21 View Post
    Hello. Truly, I had to do as you say:
    However, there is a small problem here: eventFilter function gets MouseButtonRelease event twice.
    Should this be so? If so, maybe you know why this happens?
    You have installed the event filter on two objects, or may be installed event filer on same object twice.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Aug 2017
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: eventFilter anywhere in the program.

    Hmm, weird. I install event filter only in one place of code and only for one object.
    Be kind, take a look at the code of my project, and tell me where I was mistaken?

    Headers:
    first.h
    Qt Code:
    1. #ifndef FIRST_H
    2. #define FIRST_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class first : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. first(QWidget *parent = 0);
    12. ~first();
    13. };
    14.  
    15. #endif // FIRST_H
    To copy to clipboard, switch view to plain text mode 
    second.h
    Qt Code:
    1. #ifndef SECOND_H
    2. #define SECOND_H
    3.  
    4. #include <QWidget>
    5.  
    6. class second : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit second(QWidget *parent = nullptr);
    11. ~second();
    12.  
    13. int cc = 0;
    14.  
    15. virtual bool eventFilter(QObject *watched, QEvent *event);
    16. signals:
    17.  
    18. public slots:
    19. };
    20.  
    21. #endif // SECOND_H
    To copy to clipboard, switch view to plain text mode 
    Sources:
    main.cpp
    Qt Code:
    1. #include "first.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. first w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    first.cpp
    Qt Code:
    1. #include "first.h"
    2. #include "second.h"
    3. #include <QtWidgets>
    4.  
    5. first::first(QWidget *parent) : QMainWindow(parent)
    6. {
    7. second *second1 = new second(this);
    8. }
    9.  
    10. first::~first()
    11. {
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    second.cpp
    Qt Code:
    1. #include "second.h"
    2. #include <QtWidgets>
    3.  
    4. second::second(QWidget *parent) : QWidget(parent)
    5. {
    6. qApp->installEventFilter(this);
    7. }
    8.  
    9. second::~second(){
    10.  
    11. }
    12.  
    13. bool second::eventFilter(QObject *watched, QEvent *event){
    14. if (event->type() == QEvent::MouseButtonRelease) {
    15.  
    16. cc++;
    17. qDebug() << cc;
    18.  
    19. return false;
    20. }else{
    21. return false;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter anywhere in the program.

    print out the address of the 'watched' object, see if both events belong to the same object.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Aug 2017
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: eventFilter anywhere in the program.

    Hello. I already did it. But I have determined that events belong to different objects.
    After this code:

    Qt Code:
    1. cc++;
    2. qDebug() << cc;
    To copy to clipboard, switch view to plain text mode 
    I added the following code:
    Qt Code:
    1. qDebug() << watched->metaObject()->className();
    To copy to clipboard, switch view to plain text mode 

    Received a result:
    Forum_question_2_1.png

    Therefore, in eventFilter, I added the following kind of verification:
    Qt Code:
    1. QString tempStr = "QWidgetWindow";
    2. if (watched->metaObject()->className() == tempStr){
    3. // code here
    4. }
    To copy to clipboard, switch view to plain text mode 

    In this case, the issue is resolved. But I do not know if it is correct to do so?

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter anywhere in the program.

    But I do not know if it is correct to do so?
    Depends what your definition of "correct" is.
    I would suggest however to not install the event filter on qApp, but on the actual object which of whom you want to filter events, in this case you 'first' object.
    This will save you the need to "filter" your objects based on class name which is not optimal (what if you have several objects of the same class?)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Aug 2017
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: eventFilter anywhere in the program.

    Quote Originally Posted by high_flyer View Post
    (what if you have several objects of the same class?)
    For each object, I can say a unique ObjectName which allows me to know from which object I get the events.
    For example:
    Qt Code:
    1. QApplication a(argc, argv);
    2. first w;
    3. w.setObjectName("Lollipop");
    4. w.show();
    To copy to clipboard, switch view to plain text mode 
    In the eventFilter:
    Qt Code:
    1. bool second::eventFilter(QObject *watched, QEvent *event){
    2. QString tempStr = "LollipopWindow";
    3. if(watched->objectName() == tempStr){
    4. //Code here
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    However, it should be remembered that to the ObjectName is added string "Window". Why this happens, I do not know.

    Quote Originally Posted by high_flyer View Post
    I would suggest however to not install the event filter on qApp, but on the actual object which of whom you want to filter events, in this case you 'first' object.
    The following code:
    Qt Code:
    1. qDebug() << watched->metaObject()->className();
    To copy to clipboard, switch view to plain text mode 
    returns the following string: "QWidgetWindow", after him "first", after - "second". Why on the "first" should I set the event filter?
    Perhaps "QWidgetWindow" is the very first class? And that it should be set to filter events?
    But, I do not find a class with that name.
    Forum_question_2_2.png
    What is this class, "QWidgetWindow"? How to find it? Where can I find out about it?

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter anywhere in the program.

    For each object, I can say a unique ObjectName which allows me to know from which object I get the events.
    Sure you *can* do it, but this is hardly good practice, it makes the code very rigid and you are ignoring the fact that when you have the pointer 'watched' it already points to a known (to you) object, so why bother with setting and querying strings when you can simply compare addresses (when you insist to filter on the application level)?
    It would more generic, and less rigid and robust to changes you might have later in your code.

    I could not understand the text below the qDebug() snippet, sorry.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Aug 2017
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: eventFilter anywhere in the program.

    Quote Originally Posted by high_flyer View Post
    I could not understand the text below the qDebug() snippet, sorry.
    I apologize for the wrong text. I wanted to say that qDebug () displays the following values:
    Forum_question_2_3.png
    I am interested in the QWidgetWindow value, which is marked in orange.
    To which class is this name? Can I install event filter for an object of this class?

    Again I apologize for the incomprehensible text.

Similar Threads

  1. Eventfilter delay
    By tvj4218 in forum Newbie
    Replies: 1
    Last Post: 8th July 2017, 16:31
  2. EventFilter problem
    By aruval3 in forum Qt Programming
    Replies: 10
    Last Post: 17th November 2011, 21:22
  3. Autorepeat within eventfilter
    By Patrick_Bao in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2010, 07:37
  4. sceneEventFilter or eventFilter
    By zgulser in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 07:50
  5. eventFilter in Console App
    By KaptainKarl in forum Newbie
    Replies: 3
    Last Post: 20th December 2008, 00:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.