Results 1 to 3 of 3

Thread: How to ignore mousePressEvent when getting doubleClickEvent?

  1. #1
    Join Date
    Jun 2007
    Posts
    15
    Thanks
    2

    Question How to ignore mousePressEvent when getting doubleClickEvent?

    I am developing a small application and creating some QToolButton object( by subclassing). In that class, I want to handle MousePressEvent and MouseDoubleClick Event separately but when I double click by mouse, I always receive MousePressEvent before MouseDoubleClick. I only want to receive Mouse Double Click Event and ignore MousePressEvent when I double click!

    Any help?

    Thanks!

  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: How to ignore mousePressEvent when getting doubleClickEvent?

    Yeah, no support for that.
    You can do it with a QTime. In the mousePressEvent, when the first click comes, initialize a QTime member to QTime::currentTime.
    In any subsequent click events compare QTime::currentTime with the previously saved one(you can compare by milisecond). If the difference is smaller than 800ms or even 500ms(the user would have to be very fast) then you have a double click and you can emit a custom doubleClick() signal or whatever.

  3. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to ignore mousePressEvent when getting doubleClickEvent?

    Quote Originally Posted by marcel View Post
    Yeah, no support for that.
    If the difference is smaller than 800ms or even 500ms(the user would have to be very fast) then you have a double click and you can emit a custom doubleClick() signal or whatever.
    Hardcoding a double click time is not ideal. Use QApplication::doubleClickInterval() to get the correct number of milliseconds.

    Also this doesn't really solve the problem of how to ignore the mouse press if there is a double click coming. Basically it's impossible to determine on mouse press if another click is coming, since it obviously hasn't happened yet. However, if a small delay is acceptable, you can do the following:

    In your mousePressEvent, start a timer with a timeout of QApplication::doubleClickInterval() + a little bit, where a little bit is some small number of milliseconds as a buffer.

    If the user clicks just once, then you can execute whatever action you want when the timer finishes. If you get a mouseDoubleClick before your timer finishes, you can cancel the timer and execute the double click action.

    Like I said, the only downside is that there will be about a 400 ms delay when responding to single clicks.

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
  •  
Qt is a trademark of The Qt Company.