PDA

View Full Version : Stopping mouseReleaseEvent when capturing mouseDoubleClickEvent



Jeffb
18th August 2011, 07:46
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

stampede
18th August 2011, 09:13
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.

Jeffb
18th August 2011, 09:30
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.)

stampede
18th August 2011, 09:55
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:


class Widget : public QWidget{
Q_OBJECT
public:
Widget( QWidget * parent = NULL ) : QWidget(parent){
_timer.setInterval(200);
_timer.setSingleShot(true);
connect(&_timer, SIGNAL(timeout()), this, SLOT(timeout()));
_doubleClicked = false;
}
protected slots:
void timeout(){
// do the stuff for single click() here
qDebug() << "single click";
}
protected:
void mousePressEvent( QMouseEvent * ev ){
if( _doubleClicked ){
_doubleClicked = false;
} else{
_timer.start();
}
QWidget::mousePressEvent(ev);
}
void mouseDoubleClickEvent( QMouseEvent * ev ){
_timer.stop();
_doubleClicked = true; // this is to discard another press event coming
qDebug() << "double click";
QWidget::mouseDoubleClickEvent(ev);
}
private:
QTimer _timer;
bool _doubleClicked;
};

Jeffb
18th August 2011, 13:30
Thanks Stampede

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

Cheers
Jeff

stampede
18th August 2011, 13:38
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.

Jeffb
19th August 2011, 11:45
This should fix it:


timer.setInterval(QApplication::doubleClickInterva l() + 100);

and


else if (!timer.isActive())
timer.start();

Jeff