PDA

View Full Version : How to ignore mousePressEvent when getting doubleClickEvent?



dungsivn
1st November 2007, 08:40
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!

marcel
1st November 2007, 08:46
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.

pherthyl
1st November 2007, 09:25
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.