PDA

View Full Version : Qt5 - mouseDoubleClickEvent



SteveH
6th March 2013, 22:34
Hi all,

I'm porting a number of progs from Qt4 to Qt5 on mswindows and cannot get the mouse double click event to fire on qt5.

I setup a simple mainwindow widget with an event catcher:

bool MainWindow::event(QEvent *event)
{
if((event->type() >= 2) && (event->type() <= 5))
qDebug() << (int)event->type() ;
}

but it only lists press, release, press, release type codes for double clicks. I've tried it on XP and Win7 - both same result

I've noticed on various forums chatter that the double click event was altered in qt5 to make it compatable with some other os's

Is there now a setup option that needs to be configured for Qt5 to enable double clicks or do I need to use a timer to build my own double click detector ?


SteveH

SteveH
7th March 2013, 22:31
Still no joy finding any references to problem - either it's so obvious that no one has wrote about it or it's still a problem.

Either way I simply used a QTimer and a few lines of code in a mousePressEvent to get around the problem. Also this way it seperates out single and double presses as exclusive events rather than giving single press or single press + doubleclick events.

wysota
7th March 2013, 23:58
I don't see any magic in the source code. Qt simply checks the double-click interval time reported by the style and if two subsequent mouse presses are within that interval, it sends a double click event.


ulong doubleClickInterval = static_cast<ulong>(qApp->styleHints()->mouseDoubleClickInterval());
doubleClick = e->timestamp - mousePressTime < doubleClickInterval && button == mousePressButton;

// ...

if (doubleClick) {
mousePressButton = Qt::NoButton;
const QEvent::Type doubleClickType = frameStrut ? QEvent::NonClientAreaMouseButtonDblClick : QEvent::MouseButtonDblClick;
QMouseEvent dblClickEvent(doubleClickType, localPoint, localPoint, globalPoint,
button, buttons, e->modifiers);
dblClickEvent.setTimestamp(e->timestamp);
QGuiApplication::sendSpontaneousEvent(window, &dblClickEvent);
}

Maybe you are getting NonClientAreaMouseButtonDblClick and not MouseButtonDblClick?


Either way I simply used a QTimer and a few lines of code in a mousePressEvent to get around the problem. Also this way it seperates out single and double presses as exclusive events rather than giving single press or single press + doubleclick events.

So with a single click you first get mouse release event and then mouse press event? :)

norobro
8th March 2013, 05:38
Not trying to hijack the thread but this is apropos to the OP.

If I create (QtCreator 2.6.2) a simple "MainWindow" project and override QMainWindow::event() with SteveH's code above I get the following when double clicking:

Compiled with Qt 4.8.0:
mouseButtonPress
mouseButtonRelease
mouseButtonDblClick
mouseButtonRelease

The same code compiled against Qt 5.0.1 produces only :
mouseButtonPress
mouseButtonRelease
mouseButtonPress
mouseButtonRelease

Yet the following code:

#include <QtWidgets>

class MainWindow : public QMainWindow
{
bool event(QEvent *event)
{
if((event->type() >= 2) && (event->type() < 5))
qDebug() << (int)event->type() ;
return true;
}
};

int main(int argc, char *argv[]){
QApplication app(argc, argv);
MainWindow w;
w.show();
app.exec();
}
compiled against 5.0.1 produces:
mouseButtonPress
mouseButtonRelease
mouseButtonPress
mouseButtonDblClick
mouseButtonRelease

I tried stepping through the code with the debugger but got pretty lost in the sources.

Any clue as to what's going on, Wysota?

wysota
8th March 2013, 10:37
Yes, he didn't "return true" from his code :) Thus the first press might have gotten propagated to the parent (if any) and subsequent press event didn't manage to get associated with the first one. Furthermore it is crucial to always call the base class implementation. In your codes this is not important as nothing interesting happens in QWidget::event() that is related to mouse events but in a general case not calling that method could break things.

norobro
8th March 2013, 17:45
Okay, I guess i am hijacking the thread. :)

I agree with what you said. I changed the code to forward the event to the base class, but still no double click event in a QMainWindow project.

Furthermore QWidget and QDialog class based projects do detect a double click.

I am just curious as to why the behavior is different.

SteveH
8th March 2013, 20:58
Thanks wysota and norobro for your replies.

1 - My error not setting return code in my example (cut & paste from a void type event), adding a 'true' return code has not changed the results

2 - I've tried this on 3 different computers running xp/win7 with Qt4.8 & 5.0.1 & creator 2.6.2 development kit - all give same results

3 - My old program (which runs my karting business) was ok under Qt4.8 on all pc's - it just won't recognise double clicks on Qt5

4 - My solution using a QTimer is a bit of a hack - ok it seperates single and double clicks but the single click wont be returned until the timer timesout (which the user can definately notice and is usually after the release event) and I have to save any event variables needed of first press. This hack works for the above program in the way it's being used by the staff and gives me breathing space to delve deeper or wait for a better solution from more experianced heads.

wysota
8th March 2013, 20:59
Most likely because you were clicking the central widget and trying to implement clicking in mainwindow itself but it is an academic discussion. Last time you said it worked on QMainWindow so I'm not even sure what we're talking about.

norobro
8th March 2013, 21:18
Most likely because you were clicking the central widget and trying to implement clicking in mainwindowYep, that's it. But the central widget forwarded the event to the mainwindow in 4.8. As SteveH points out, that has been changed in 5.0.