Results 1 to 7 of 7

Thread: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

  1. #1
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    Hi Guys
    I'm creating a custom QLabel class.

    I'm reimplementing mouseReleaseEvent to respond to a user click and mouseDoubleClickEvent to respond to a user doubleclick.

    When a user double clicks, it causes the both the mouseReleaseEvent and mouseDoubleClickEvent to be triggered.
    How do I stop the mouseReleaseEvent from triggering when a user doubleClicks on the label?

    Cheers
    Jeff

    Thanks
    Jeff

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    I'm reimplementing mouseReleaseEvent to respond to a user click
    I think you should use mousePressEvent instead.
    How do I stop the mouseReleaseEvent from triggering when a user doubleClicks on the label?
    For example - set a flag in mouseDoubleClickEvent, and read it in mouseReleaseEvent. If its true, then clear it and ignore() the event.

  3. #3
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    I don't think that will work as the mousePressEvent will still get triggered with the mouseDoubleClickEvent.

    I just found this in the Qt Manual:
    mouseDoubleClickEvent() is called when the user double-clicks in the widget. If the user double-clicks, the widget receives a mouse press event, a mouse release event and finally this event instead of a second mouse press event. (Some mouse move events may also be received if the mouse is not held steady during this operation.) It is not possible to distinguish a click from a double-click until the second click arrives. (This is one reason why most GUI books recommend that double-clicks be an extension of single-clicks, rather than trigger a different action.)

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    I know that, but using mouseReleaseEvent wont help you either - before you receive mouseDoubleClickEvent there will be mousePressEvent and mouseReleaseEvent (and another "press" and "release" after double click). So my previous suggestion is stupid.
    What about using the timer, something like this:
    Qt Code:
    1. class Widget : public QWidget{
    2. Q_OBJECT
    3. public:
    4. Widget( QWidget * parent = NULL ) : QWidget(parent){
    5. _timer.setInterval(200);
    6. _timer.setSingleShot(true);
    7. connect(&_timer, SIGNAL(timeout()), this, SLOT(timeout()));
    8. _doubleClicked = false;
    9. }
    10. protected slots:
    11. void timeout(){
    12. // do the stuff for single click() here
    13. qDebug() << "single click";
    14. }
    15. protected:
    16. void mousePressEvent( QMouseEvent * ev ){
    17. if( _doubleClicked ){
    18. _doubleClicked = false;
    19. } else{
    20. _timer.start();
    21. }
    22. QWidget::mousePressEvent(ev);
    23. }
    24. void mouseDoubleClickEvent( QMouseEvent * ev ){
    25. _timer.stop();
    26. _doubleClicked = true; // this is to discard another press event coming
    27. qDebug() << "double click";
    28. QWidget::mouseDoubleClickEvent(ev);
    29. }
    30. private:
    31. QTimer _timer;
    32. bool _doubleClicked;
    33. };
    To copy to clipboard, switch view to plain text mode 

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

    Jeffb (18th August 2011)

  6. #5
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    Thanks Stampede

    That looks pretty good.
    I'll give it a try.

    Cheers
    Jeff

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    No problem.
    I think this implementation has a problem - when you click first time, then click second time after the timer has already timed-out and before the doubleClickInterval() mseconds, I think you will get clicked() and doubleClicked() calls with just two mouse clicks. Should be rare (depends on the doubleClickInterval() value), but can happen sometimes. I think you can solve this yourself.

  8. #7
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent

    This should fix it:

    Qt Code:
    1. timer.setInterval(QApplication::doubleClickInterval() + 100);
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. else if (!timer.isActive())
    2. timer.start();
    To copy to clipboard, switch view to plain text mode 

    Jeff

Similar Threads

  1. MouseDoubleClickEvent on a QGraphicsScene
    By paolom in forum Qt Programming
    Replies: 6
    Last Post: 6th October 2009, 12:01
  2. mouseDoubleClickEvent on QGraphicsItem's
    By been_1990 in forum Qt Programming
    Replies: 11
    Last Post: 7th August 2009, 03:52
  3. mouseDoubleClickEvent in QGraphicsItemGroup
    By dima in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2009, 08:01
  4. Stopping QTimer
    By drinu21 in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2008, 16:59
  5. Problem of mouseDoubleClickEvent
    By Sarma in forum Qt Programming
    Replies: 18
    Last Post: 8th March 2006, 07:31

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.