Results 1 to 12 of 12

Thread: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

  1. #1
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    I am working on openCV with Qt.. I am using my hand to move the cursor instead of the mouse. Can you pls help me with the following doubt:
    I want to perform an action when the cursor comes over a push button BUT WITHOUT CLICKING IT. How do I do it?
    Thanks in advance.
    Last edited by learner_qt; 4th February 2013 at 09:43.

  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: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Create a custom QPushButton, implement mouseMoveEvent ( QMouseEvent * event ), and also enable mouse tracking
    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. The following user says thank you to Santosh Reddy for this useful post:

    learner_qt (5th February 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    As a small side note: Since mouseMoveEvent is triggered every time the mouse moves inside a widget I would prefer enterEvent in this case.

  5. The following user says thank you to Lykurg for this useful post:

    learner_qt (5th February 2013)

  6. #4
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Thank you!

  7. #5
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Quote Originally Posted by Lykurg View Post
    As a small side note: Since mouseMoveEvent is triggered every time the mouse moves inside a widget I would prefer enterEvent in this case.
    Can you help me with the enterEvent implementation??
    I am unable to proceed.
    I dont know where to start..

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Subclass QPushButton and reimplement the enterEvent where you emit the clicked signal.

    Look at the samples of the documentation, there you will find similar problems, where Qt classes where subclassed and event handlers reimplemented. If you have problems, show us what you have tried and we will go on.

  9. The following user says thank you to Lykurg for this useful post:

    learner_qt (7th February 2013)

  10. #7
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Quote Originally Posted by Lykurg View Post
    Subclass QPushButton and reimplement the enterEvent where you emit the clicked signal.

    Look at the samples of the documentation, there you will find similar problems, where Qt classes where subclassed and event handlers reimplemented. If you have problems, show us what you have tried and we will go on.
    Thanks. Here is my code.
    custom.h
    Qt Code:
    1. #ifndef CUSTOM_H
    2. #define CUSTOM_H
    3.  
    4. #include <QWidget>
    5. #include <QPushButton>
    6.  
    7. namespace Ui {
    8. class custom;
    9. }
    10.  
    11. class Custom : public QPushButton
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Custom(QWidget *parent = 0);
    17. ~Custom();
    18. void enterEvent(QEvent * );
    19. void display();
    20.  
    21. private:
    22. Ui::custom *ui;
    23.  
    24. public slots:
    25. void display();
    26. };
    27.  
    28. #endif // CUSTOM_H
    To copy to clipboard, switch view to plain text mode 

    custom.cpp
    Qt Code:
    1. #include "custom.h"
    2. #include "ui_custom.h"
    3. #include <QMessageBox>
    4. #include<QPushButton>
    5.  
    6. Custom::Custom(QWidget *parent) :
    7. QPushButton (parent),
    8. ui(new Ui::custom)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. void Custom::enterEvent(QEvent *)
    14. {
    15. Q_emit clicked();
    16. }
    17.  
    18. void Custom::display()
    19. {
    20. msg.setText("CLICKED");
    21. msg.exec();
    22. }
    23.  
    24. Custom::~Custom()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

    It works when i click that push button. How do i do that event occurs when cursor comes over that particular push button (but without click)????
    Also, i am able to do that message box is displayed when cursor comes over widget, but not over push button.
    Where am i wrong??

  11. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    It is Q_EMIT and you should leave enterEvent protected. And why the heck do you have a ui file?
    Qt Code:
    1. class Custom : public QPushButton
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Custom(QWidget* parent = 0) : QPushButton(parent)
    7. {
    8. connect(this, SIGNAL(clicked()), this, SLOT(display()));
    9. }
    10. ~Custom() {}
    11.  
    12. protected:
    13. void enterEvent(QEvent*)
    14. {
    15. Q_EMIT clicked();
    16. }
    17.  
    18. public slots:
    19. void display()
    20. {
    21. setText("clicked");
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

  12. #9
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    I am taking input from camera, moving the mouse cursor through hand gesture, and have to perform a click like function when i keep my mouse cursor over the push button for 1500 ms. For now i am trying to see whether i can call an event (without clicking) when cursor comes over push button.
    i need ui for this.

  13. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Yes, but you don't need it in a subclassed QPushButton!

  14. The following user says thank you to Lykurg for this useful post:

    learner_qt (10th February 2013)

  15. #11
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Thank you for your quick replies!!
    i did it and its working.!
    the scenario is that the user sees multiple push buttons on the screen together with a live video feed from the camera.
    and what i want to do is when he moves the cursor (through the gesture which i am tracking) to the button a specific event occurs.
    pls help me through this.

  16. #12
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT

    Done. Thanks..
    Last edited by learner_qt; 8th February 2013 at 14:31.

Similar Threads

  1. Action using Push Button ?
    By steve.bush in forum Newbie
    Replies: 7
    Last Post: 25th February 2016, 15:36
  2. Replies: 5
    Last Post: 27th May 2011, 02:51
  3. Push Button with image?
    By steve.bush in forum Newbie
    Replies: 10
    Last Post: 18th March 2011, 06:48
  4. [QT]Push button-no action
    By nqn in forum Newbie
    Replies: 4
    Last Post: 30th May 2010, 19:08
  5. Hareware Push Button
    By mickeyk191 in forum Newbie
    Replies: 2
    Last Post: 26th January 2010, 12:41

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.