Results 1 to 13 of 13

Thread: Mouse over event

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Mouse over event

    Hi all

    I am working on Qt4.2 on my Intel MAC PC.
    My Problem is that :
    -> using MainWindow having somebuttons on it .
    -> what i want is this when i move mouse over any button that is when mouse pointer is over the button it displays the details of that button in the label.
    Always Believe in Urself
    Merry

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse over event

    If you have a layout set for your main window( and you should ), you can use layout()->itemAt() and if the mouse position is inside the items geometry.
    You should do this for all the QPushButton items, in a loop.
    Anyway, even if it works, this is a wrong solution.

    The way to go is to reimplement mouseEnterEvent and mouseLeaveEvent in your buttons and emit signals when these events occur.

    Regards

  3. #3
    Join Date
    Mar 2007
    Location
    India
    Posts
    27
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse over event

    You could also see underMouse(). mouseTracking should be true, obviously. But yes, as Marcel said, reimplementing mouseEnterEvent and mouseLeaveEvent in your buttons would be neat!

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse over event

    underMouse() would involve additional polling in the main window .

    Actually the events I was talking about are called enterEvent and leaveEvent ( my bad ).

    Regards

  5. #5
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Mouse over event

    Thanx Marcel

    I tried enter event and leave event It works for the main window,Can you explain me it for the QPushButtons with example
    Always Believe in Urself
    Merry

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse over event

    Qt Code:
    1. class MyButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. MyButton( QWidget* = NULL );
    6. ~MyButton();
    7.  
    8. protected:
    9. virtual void enterEvent( QEvent* );
    10. virtual void leaveEvent( QEvent* );
    11.  
    12. public signals:
    13. void enter( QString );
    14. void leave( QString );
    15.  
    16. }
    17.  
    18. void MyButton::enterEvent( QEvent* e )
    19. {
    20. emit enter( text() );
    21. }
    22.  
    23. void MyButton::leaveEvent( QEvent* e )
    24. {
    25. emit leave( text() );
    26. }
    To copy to clipboard, switch view to plain text mode 

    This is a minimal example of what you can do.
    When a button is hovered, it emits the enter signal, with the button name as parameter. You can use the name to display it in your label.

    Also, when the mouse leaves the button, the leave signal is emitted.

    Regards

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse over event

    You can achieve a simmilar effect without any hacks. Main windows have a status bar and you can make it that it displays the text you want when a widget is hovered. It's enough to fill the statusTip property for the buttons or their actions. An alternative of course is to use a tooltip.

  8. #8
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Mouse over event

    Nothing Works , Please tell me Is there any other way to do this.
    Always Believe in Urself
    Merry

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse over event

    Maybe you'd like to tell us some more details about this "nothing"?

  10. #10
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Mouse over event

    Yaa , Actuallly I implemented that what Marcel had told me

    Qt Code:
    1. class MyButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. MyButton( QWidget* = NULL );
    6. ~MyButton();
    7.  
    8. protected:
    9. virtual void enterEvent( QEvent* );
    10. virtual void leaveEvent( QEvent* );
    11.  
    12. public signals:
    13. void enter( QString );
    14. void leave( QString );
    15.  
    16. }
    17.  
    18. void MyButton::enterEvent( QEvent* e )
    19. {
    20. emit enter( text() );
    21. }
    22.  
    23. void MyButton::leaveEvent( QEvent* e )
    24. {
    25. emit leave( text() );
    26. }
    To copy to clipboard, switch view to plain text mode 


    and also what u told
    , that Main windows have a status bar and you can make it that it
    displays the text you want when a widget is hovered.
    But it wont works ,because what i want is this ,I am having 6 Push buttons on my main window,and each works differently ,When mouse cursor is over any button it displays the detail of that button in the label that i used.
    Last edited by wysota; 16th May 2007 at 10:54. Reason: Incorrect tags
    Always Believe in Urself
    Merry

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse over event

    Have you used MyButton instead of QPushButton?
    Also have you used the two signals properly?

    It is impossible not to
    work, therefore you have done something wrong.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse over event

    I got this to work without any subclassing, just using a status tip.
    Attached Files Attached Files

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

    merry (16th May 2007)

  14. #13
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Mouse over event

    Thanx Wysota it works and also thanx to Marcel for helping me so much
    Always Believe in Urself
    Merry

Similar Threads

  1. rotation and mouse event identification
    By kiransu123 in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2007, 04:41
  2. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  3. Mouse Over event on button
    By vishal.chauhan in forum Qt Programming
    Replies: 9
    Last Post: 10th January 2007, 05:03
  4. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 09:24
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

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.