Results 1 to 5 of 5

Thread: Advise sought for implementation of hoverEvent in QWidget

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Advise sought for implementation of hoverEvent in QWidget

    I have QWidget-derived class where I am painting custom view. I’d like the user to be able to move the mouse pointer to a certain position within the view boundaries. When the mouse pointer reaches this point and user stops I need to display the dialog.

    I don’t think I can use QAction because my view is not a toolbar or a menu. So, I turned to events and this is my current implementation. The problem is that these events are generated all the time. I can’t figure out a way to find the time to actually show my dialog – events are too “small” to detect specific time when the mouse is actually stopped moving.

    Your advise will be greatly appreciated.

    Qt Code:
    1. myView::myView (QWidget *parent): QWidget(parent)
    2. {
    3. this->setAttribute(Qt::WA_Hover);
    4. }
    5.  
    6. bool myView::event(QEvent *event)
    7. {
    8. if (event->type() == QEvent::HoverMove)
    9. {
    10. QHoverEvent * ke = static_cast<QHoverEvent*>(event);
    11. if (ke->oldPos() == QPoint(-1,-1))
    12. return QWidget::event(event);
    13. QPoint delta = ke->oldPos() - ke->pos();
    14. if ( (std::abs(delta.x()) < 2) && (std::abs(delta.y()) < 2) )
    15. {
    16. //show dialog here
    17. return true;
    18. }
    19.  
    20. }
    21. return QWidget::event(event);
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Advise sought for implementation of hoverEvent in QWidget

    You'll need to decide what you mean by "stopped moving" - how long does the mouse have to remain motionless to qualify? And is just being within the hot zone sufficient, or must the mouse be completely motionless within that area?

    Anyway, one approach would be to use a QTimer set to your timeout interval, and reset the timer with each mouse move. If the timer ever times out, it'll emit a signal which you can connect to your popup dialog.

    Personally, I think it would be better - and simpler - to just pop up the dialog when the mouse is within the hot zone, and ignore timing and whether the mouse has "stopped." But this is a rough guide if you decide to proceed.

  3. The following user says thank you to SixDegrees for this useful post:

    TorAn (1st June 2010)

  4. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Advise sought for implementation of hoverEvent in QWidget

    Thank you, SixDegrees. I was considering the timer, but thought that Qt might offer something already built-in which I overlooked. After all, QAction seems to have this functionality already. If there are no other suggestions from the readers of this thread - QTimer will be my choice

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Advise sought for implementation of hoverEvent in QWidget

    Your need seems to be something like showing a dialog instead of tooltip. Hope I get it right.
    So you can hack the tooltip implementation and show your dialog.

    You will need to check for the QEvent::ToolTip

  6. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Advise sought for implementation of hoverEvent in QWidget

    Quote Originally Posted by aamer4yu View Post
    Your need seems to be something like showing a dialog instead of tooltip. Hope I get it right.
    So you can hack the tooltip implementation and show your dialog.

    You will need to check for the QEvent::ToolTip
    You description is correct and your suggestion is exactly what I needed. Added it and got the desired effect. Thank you.

    if (event->type() == QEvent::ToolTip)
    ...

Similar Threads

  1. Replies: 2
    Last Post: 3rd May 2010, 13:18
  2. advise sought on TreeView model design
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 25th June 2009, 15:36
  3. Replies: 6
    Last Post: 25th February 2008, 22:18
  4. QGraphicsItem HoverEvent Doubt
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 18:40
  5. Help sought on console + GUI Integration...
    By mysearch05 in forum Newbie
    Replies: 7
    Last Post: 28th January 2006, 15: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.