Results 1 to 14 of 14

Thread: Event filter with mouse events

  1. #1
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Event filter with mouse events

    Hi everyone.
    Firstly sorry for my english.

    I have some problem with qt so i decidied to ask You for help.

    I'writting a program. I want to do somethink like this:
    - when i move mouse on button, for example, should show table with data like name.
    The main difficult to me is that the button is on MainWindow and if i want to show new table when the mouse is on button i must create new class or somethink like this.

    I rode a lot about QMouseEvent and event filter but i stuck i don't know what i should do next.

    1. Firts way i used event filter. Some code:.cpp
    Qt Code:
    1. bool MainWindow::eventFilter(QObject *object, QEvent *ev)
    2. {
    3. if (object == pushButton)
    4. {
    5. if(ev->type() == QEvent::Enter)
    6. {
    7. QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
    8. return true;
    9. }
    10. else
    11. {
    12. return false;
    13. }
    14. }
    15. else
    16. {
    17. QApplication::restoreOverrideCursor();
    18. return false;
    19. }
    20. }
    21. ...
    22. ...
    23. MainWindow::MainWindow(QWidget *parent) :
    24. QMainWindow(parent),
    25. ui(new Ui::MainWindow)
    26. {
    27. ui->setupUi(this);
    28. pushButton->installEventFilter(this);
    29. }
    To copy to clipboard, switch view to plain text mode 
    I set a type of cursor because i wanted to see it is working.
    .h
    Qt Code:
    1. ...
    2. protected:
    3. bool eventFilter(QObject *object, QEvent *ev);
    4. ...
    To copy to clipboard, switch view to plain text mode 
    This method isn't working. When I press Run button the application crash.

    2.The second way is use EnterEvent and LeaveEvent. For that i create a new .h file and called him FlatButton when i declared:
    .h
    Qt Code:
    1. ...
    2. protected:
    3. void enterEvent(QEvent *ev);
    4. void leaveEvent(QEvent *ev);
    5. ...
    To copy to clipboard, switch view to plain text mode 
    In cpp file:
    Qt Code:
    1. ...
    2. void FlatButton::enterEvent(QEvent *ev)
    3. {
    4. QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
    5. }
    6. void FlatButton::leaveEvent(QEvent *ev)
    7. {
    8. QApplication::restoreOverrideCursor();
    9. }
    10. ...
    To copy to clipboard, switch view to plain text mode 
    When i replace FlatButton:: on MainWindow:: it's working but the cursor change type when enter or live MainWindow but i want to change him only on button. For this reason i created FlatButton.

    I don't know where i do wrong. I would be grateful for help.

  2. #2
    Join Date
    Apr 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Event filter with mouse events

    Why don't you try this?

    In enterEvent
    Qt Code:
    1. this->enterEvent(ev);
    To copy to clipboard, switch view to plain text mode 

    In leaveEvent
    Qt Code:
    1. this->leaveEvent(ev);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Event filter with mouse events

    When I press Run button the application crash.
    Where does it crash? on what line?
    ==========================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.

  4. #4
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Event filter with mouse events

    Quote Originally Posted by Furkan View Post
    Why don't you try this?

    In enterEvent
    Qt Code:
    1. this->enterEvent(ev);
    To copy to clipboard, switch view to plain text mode 

    In leaveEvent
    Qt Code:
    1. this->leaveEvent(ev);
    To copy to clipboard, switch view to plain text mode 
    Where should i paste it? I paste it
    Qt Code:
    1. void FlatButton::enterEvent(QEvent *ev)
    2. {
    3. this->enterEvent(ev);
    4. QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
    5. }
    6. void FlatButton::leaveEvent(QEvent *ev)
    7. {
    8. this->leaveEvent(ev);
    9. QApplication::restoreOverrideCursor();
    10. }
    To copy to clipboard, switch view to plain text mode 
    But it doesn't works.
    Quote Originally Posted by high_flyer View Post
    Where does it crash? on what line?
    When i press Run the program compile with any errors or warnings but shows a Windows message box somethink like this one http://edu.i-lo.tarnow.pl/inf/hist/0.../win7crash.png. I think it's depend on
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. pushButton->installEventFilter(this);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 
    When the pushButton->installEventFilter(this) is not enabled the crash doesn't appear.

  5. #5
    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: Event filter with mouse events

    but shows a Windows message box somethink like this one
    The windows manager crashes when you start debugging??
    Then something is very wrong with your system!
    ==========================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.

  6. #6
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Event filter with mouse events

    Quote Originally Posted by high_flyer View Post
    The windows manager crashes when you start debugging??
    Then something is very wrong with your system!
    It's only crash when the pushButton->installEventFilter(this) is enabled. After crash system working normally. Only crash MyProgramInQT.exe not a Windows.

  7. #7
    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: Event filter with mouse events

    Only crash MyProgramInQT.exe not a Windows.
    The screenshot you posted says your window manager crashed, so either its not the message you get, or, as I said, your windows managers is crashing.
    ==========================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.

  8. #8
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Event filter with mouse events

    Quote Originally Posted by felo188 View Post
    shows a Windows message box somethink like this
    I mean message box only looking like this one. My message says that MyProgramInQT.exe stops working not a system Windows

  9. #9
    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: Event filter with mouse events

    I mean message box only looking like this one. My message says that MyProgramInQT.exe stops working not a system Windows
    What is the point of not giving the exact information? do you want help or not?
    Sending people on the wrong track will not help you!
    Are you sure your project is in debug mode?
    It sounds like you are starting a release mode project.
    Make sure you are in debug mode, and start the debugger, it will then stop on the line that is crashing.
    Post that line here, and then we can start seeing where the problem is.
    ==========================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.

  10. #10
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Event filter with mouse events

    Sorry for misleading.

    When i click Start Debugging (F5) debug stops at this
    The code from Compile Output:

    Running build steps for project Mysz...
    Configuration unchanged, skipping qmake step.
    Starting: "E:\Programy\QtSDK\mingw\bin\mingw32-make.exe"
    E:/Programy/QtSDK/mingw/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory `C:/Users/Lukasz/Desktop/Mysz-build-desktop'
    mingw32-make[1]: Nothing to be done for `first'.
    mingw32-make[1]: Leaving directory `C:/Users/Lukasz/Desktop/Mysz-build-desktop'
    The process "E:\Programy\QtSDK\mingw\bin\mingw32-make.exe" exited normally.

  11. #11
    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: Event filter with mouse events

    please stop post external links with screenshots.
    Attach the screenshots to the post, since after some time the external links stop being valid, and the post loses information, which might be important for people reading this in later time.
    Also, for posting a section of code you can simple cope the code and paste it here with [code] tags.

    Where is 'pushButton' defined, and where do you initialize 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.

  12. #12
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Event filter with mouse events

    It's a screenshot window.jpg.

    I didn't have a defined pushButton so i added it:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. QPushButton *pushButton = new QPushButton("button1",this);
    7. pushButton->installEventFilter(this);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Now the program stops crashing.
    I'm wondering why the event filter don't working.
    Qt Code:
    1. bool MainWindow::eventFilter(QObject *object, QEvent *ev)
    2. {
    3. if ((object == pushButton) && ev->type() == QEvent::Enter)
    4. {
    5. QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
    6. return true;
    7. }
    8. if ((object == pushButton) && ev->type() == QEvent::Leave)
    9. {
    10. QApplication::restoreOverrideCursor();
    11. return true;
    12. }
    13. else
    14. return false;
    15. }
    To copy to clipboard, switch view to plain text mode 
    When the cursor is on button he should change a type to cross but he don't change.

  13. #13
    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: Event filter with mouse events

    I didn't have a defined pushButton so i added it:
    Thats is not true.
    If there was not a defined 'pushButton' you would have gotten a compile error.
    Which is also the reason your event handler "is not working".
    You have a defined a local 'pushButton' in your constructor, which is being initialized, but the 'pushButton' in your evnetFilter is declared somewhere else, probably in your header in class scope, but it is not initialized, hence it never matcher your if() statements.
    It seems you have no idea how classes in C++ work, and what are members.
    Hence, this is becoming off topic for this forum.
    ==========================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.

  14. #14
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Event filter with mouse events

    I solved my problem.
    .h
    Qt Code:
    1. ...
    2. QPushButton *pushButton;
    3. ....
    To copy to clipboard, switch view to plain text mode 
    and .cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. pushButton = new QPushButton("button1",this);
    7. pushButton->setGeometry(50,45,60,25);
    8. pushButton->installEventFilter(this);
    9. }
    To copy to clipboard, switch view to plain text mode 
    high_flyer anyway thanks for help.

Similar Threads

  1. Event filter question
    By d_stranz in forum Qt Programming
    Replies: 7
    Last Post: 7th July 2011, 23:08
  2. Find QObjects that insatlled event filter
    By babu198649 in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2010, 11:27
  3. problem with event filter compile for S60
    By jimiq in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 6th July 2010, 19:48
  4. Event Filter & No Focus problem
    By AlexanderPopov in forum Newbie
    Replies: 0
    Last Post: 22nd December 2009, 20:15
  5. Q3Table event filter problem
    By batileon in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 10:40

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.