+ Reply to Thread
Results 1 to 18 of 18

Thread: eventFilter anywhere in the program.

  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,878
    Thanks
    3
    Thanked 451 Times in 434 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 20:41.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,878
    Thanks
    3
    Thanked 451 Times in 434 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,620
    Thanks
    21
    Thanked 405 Times in 399 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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  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,620
    Thanks
    21
    Thanked 405 Times in 399 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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  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,620
    Thanks
    21
    Thanked 405 Times in 399 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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  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.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,620
    Thanks
    21
    Thanked 405 Times in 399 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter anywhere in the program.

    It seems QWidgetWindow is some sort of a non documented helper class, and it derives from QWindow.
    Here is a grep output on it (searched only headers):
    Qt Code:
    1. ./qtbase/src/widgets/kernel/qapplication.h:217: friend class QWidgetWindow;
    2. ./qtbase/src/widgets/kernel/qwidget.h:714: friend class QWidgetWindow;
    3. ./qtbase/src/widgets/kernel/qwidgetwindow_p.h:40:#ifndef QWIDGETWINDOW_P_H
    4. ./qtbase/src/widgets/kernel/qwidgetwindow_p.h:41:#define QWIDGETWINDOW_P_H
    5. ./qtbase/src/widgets/kernel/qwidgetwindow_p.h:67:class QWidgetWindow : public QWindow
    6. ./qtbase/src/widgets/kernel/qwidgetwindow_p.h:71: QWidgetWindow(QWidget *widget);
    7. ./qtbase/src/widgets/kernel/qwidgetwindow_p.h:72: ~QWidgetWindow();
    8. ./qtbase/src/widgets/kernel/qwidgetwindow_p.h:139:#endif // QWIDGETWINDOW_P_H
    9. ./qtbase/src/corelib/kernel/qcoreapplication.h:220: friend class QWidgetWindow;
    To copy to clipboard, switch view to plain text mode 

    Without looking into the code itself, from the grep it seems that this class is a bit like a private implementation class which Qt is heavily using (PIMPL design pattern), probably to inject some special behavior for specific events in specific cases.
    I think its safe to assume its your top level application window.

    And again, the problem with the QWidgetWindow is a good example why using the string based polling of classes or object names is bad practice and very fragile code.
    And because its part of the private Qt area also means it can change, be renames or removed in any future Qt release breaking your code.
    Last edited by high_flyer; 22nd October 2017 at 01:25.
    ==========================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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  13. The following user says thank you to high_flyer for this useful post:

    Oleg21 (26th October 2017)

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

    Default Re: eventFilter anywhere in the program.

    Hello. Thank you very much for helping me find "QWidgetWindow".

    Quote Originally Posted by high_flyer View Post
    And again, the problem with the QWidgetWindow is a good example why using the string based polling of classes or object names is bad practice and very fragile code.
    And because its part of the private Qt area also means it can change, be renames or removed in any future Qt release breaking your code.
    I absolutely agree with you. Performing a check on the name of the class is not correct.

    To better reflect my actions, I threw out unnecessary code from the project.
    Consequently, the project contains two classes: first.cpp and second.cpp.
    In the first.cpp class, is created an object of the QPushButton class "Button1".
    In the second.cpp class, is created an object of the QPushButton class "Button2".
    In the second.cpp class is installed events filter for qApp.
    In the first.cpp class is created the object of second.cpp "second1".
    Graphically I depicted it in the picture below:
    Forum_question_2_4.jpg

    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 first1;
    8. first1.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. this->resize(230, 70);
    8.  
    9. second *second1 = new second(this);
    10.  
    11. QPushButton *button1 = new QPushButton;
    12. button1->setParent(this);
    13. button1->setText("Button1");
    14. button1->setObjectName("Button1");
    15. button1->setGeometry(120, 10, 100, 25);
    16. }
    17.  
    18. first::~first()
    19. {
    20.  
    21. }
    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. resize(115, 45);
    7.  
    8. qApp->installEventFilter(this);
    9.  
    10. QPushButton *button2 = new QPushButton;
    11. button2->setParent(this);
    12. button2->setText("Button2");
    13. button2->setObjectName("Button2");
    14. button2->setGeometry(10, 10, 100, 25);
    15. }
    16.  
    17. second::~second(){
    18.  
    19. }
    20.  
    21. bool second::eventFilter(QObject *watched, QEvent *event){
    22. if (event->type() == QEvent::MouseButtonRelease) {
    23. QMouseEvent *pe = static_cast<QMouseEvent*>(event);
    24.  
    25. cc++;
    26. qDebug() << cc;
    27. qDebug() << watched->metaObject()->className() << endl;
    28.  
    29. return false;
    30. }else{
    31. return false;
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    Now when I'm clicking a mouse on any objects, i have result:

    Forum_question_2_5_1.jpg

    Now I installed event filter for "first1" (first.cpp).

    Qt Code:
    1. second::second(QWidget *parent) : QWidget(parent)
    2. {
    3. resize(115, 45);
    4.  
    5. parent->installEventFilter(this);
    6.  
    7. QPushButton *button2 = new QPushButton;
    8. button2->setParent(this);
    9. button2->setText("Button2");
    10. button2->setObjectName("Button2");
    11. button2->setGeometry(10, 10, 100, 25);
    12. }
    To copy to clipboard, switch view to plain text mode 
    "parent" is pointer to "first1" object.

    When I'm clicking a mouse on "first1" or "second1" i have result:

    Forum_question_2_5_2.png

    When I'm clicking a mouse on any buttons (button1 or button2) i dont have result. This is probably due to the fact that eventFilter does not work for them.
    This is why I want to leave eventFilter for qApp and not for other objects. I want so that eventFilter work globally for all objects.
    How to achieve this by setting eventFilter to the first.cpp class object ("first1")?

  15. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,620
    Thanks
    21
    Thanked 405 Times in 399 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter anywhere in the program.

    What happens when you do this:
    Qt Code:
    1. bool second::eventFilter(QObject *watched, QEvent *event){
    2. if (event->type() == QEvent::MouseButtonRelease) {
    3. QMouseEvent *pe = static_cast<QMouseEvent*>(event);
    4.  
    5. cc++;
    6. qDebug() << cc;
    7. qDebug() << watched->metaObject()->className() << endl;
    8.  
    9. return false;
    10. }
    11.  
    12. return QWidget::eventFileter(watched, event);
    13. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  16. #15
    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 happens when you do this:
    When I made the changes as you suggested, nothing has changed:
    Qt Code:
    1. bool second::eventFilter(QObject *watched, QEvent *event){
    2. if (event->type() == QEvent::MouseButtonRelease) {
    3. QMouseEvent *pe = static_cast<QMouseEvent*>(event);
    4.  
    5. cc++;
    6. qDebug() << cc;
    7. qDebug() << watched->metaObject()->className() << endl;
    8.  
    9. return false;
    10. }
    11.  
    12. /*else{
    13.   return false;
    14.   }*/
    15.  
    16. return QWidget::eventFilter(watched, event);
    17. }
    To copy to clipboard, switch view to plain text mode 

    when I'm clicking on "first1" or "second1" i have result:
    Forum_question_2_5_2.png
    when I'm clicking on any buttons (button1 or button2) i do not have result.

    When I made the next code:
    Qt Code:
    1. bool second::eventFilter(QObject *watched, QEvent *event){
    2. if (event->type() == QEvent::MouseButtonRelease) {
    3. QMouseEvent *pe = static_cast<QMouseEvent*>(event);
    4.  
    5. cc++;
    6. qDebug() << cc;
    7. qDebug() << watched->metaObject()->className() << endl;
    8.  
    9. return false;
    10. }
    11. return second::eventFilter(watched, event);
    12. }
    To copy to clipboard, switch view to plain text mode 
    then the program is crashed after run.

  17. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,620
    Thanks
    21
    Thanked 405 Times in 399 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter anywhere in the program.

    I still don't understand what it is you want to do.
    What is the goal you are after? which events do you need form which objects?
    If you want to filter the button events, you can install an event filter on the buttons.
    This all seems very over engineered and complex for no reason.
    Also, do you really need to fileter event by another object?
    Are you aware you can process events in the objects themselves by overloading event() or specialized event handler?

    In short: please explain what is your goal - not what you want to do (like filtering some event) - depending on the goal we can see what is the best method to achieve it.
    ==========================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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  18. #17
    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 still don't understand what it is you want to do.
    What is the goal you are after?
    Okay, I want to record actions with the program (with the interface). And save this data in log file.

    Quote Originally Posted by high_flyer View Post
    which events do you need form which objects?
    If you want to filter the button events, you can install an event filter on the buttons.
    I want to install global eventFilter, so that work with everyone object at once. I don't want to install unique eventFiletr for all objects in my app. Quantity and type of objects are unknown in advance.

    Quote Originally Posted by high_flyer View Post
    Also, do you really need to fileter event by another object?
    The idea itself is to put this code into a separate class and include it as needed (a utility class).

    Quote Originally Posted by high_flyer View Post
    Are you aware you can process events in the objects themselves by overloading event() or specialized event handler?
    Overloading event() - yes i know about it. But this will force me to overload the event() in all objects. It's complicated.

    Therefore, in my opinion, the best option of all is to use:
    Qt Code:
    1. qApp->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: eventFilter anywhere in the program.

    Hello.
    You wrote above:
    Quote Originally Posted by high_flyer View Post
    It seems QWidgetWindow is some sort of a non documented helper class, and it derives from QWindow.
    And really it is so! I read the Qt source code. Namely the header file qwidgetwindow_p.h. Here is a warning I saw in this file:

    qwidgetwindow_p.h
    Qt Code:
    1. //
    2. // W A R N I N G
    3. // -------------
    4. //
    5. // This file is not part of the Qt API. It exists purely as an
    6. // implementation detail. This header file may change from version to
    7. // version without notice, or even be removed.
    8. //
    9. // We mean it.
    10. //
    To copy to clipboard, switch view to plain text mode 
    You were right!

    So, on this basis, I firmly decided to refuse to use any parts of the code that mention the "QWidgetWindow" class.
    Therefore, I reworked my evenFilter:
    Qt Code:
    1. bool second::eventFilter(QObject *watched, QEvent *event){
    2. QPoint tempPoint(0, 0);
    3.  
    4. if (event->type() == QEvent::MouseButtonPress) {
    5. QMouseEvent *pe = static_cast<QMouseEvent*>(event);
    6.  
    7. tempPoint = pe->globalPos();
    8. QWidget *w = QApplication::widgetAt( tempPoint );
    9.  
    10. if (w == watched) {
    11. iTempCount = iTempCount + 1;
    12. qDebug() << iTempCount;
    13. qDebug() << watched->metaObject()->className();
    14. }
    15.  
    16. return false;
    17. }else{
    18. return false;
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    Indeed, this code is also not perfect. But he suits me.

    Thank you for the help provided to me. I apologize if I was rude, obtrusive or incomprehensible.
    Last edited by Oleg21; 26th October 2017 at 20:56.

+ Reply to Thread

Similar Threads

  1. Eventfilter delay
    By tvj4218 in forum Newbie
    Replies: 1
    Last Post: 8th July 2017, 17:31
  2. EventFilter problem
    By aruval3 in forum Qt Programming
    Replies: 10
    Last Post: 17th November 2011, 22:22
  3. Autorepeat within eventfilter
    By Patrick_Bao in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2010, 08:37
  4. sceneEventFilter or eventFilter
    By zgulser in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 08:50
  5. eventFilter in Console App
    By KaptainKarl in forum Newbie
    Replies: 3
    Last Post: 20th December 2008, 01: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