PDA

View Full Version : Recognise double click



Bedopies
18th October 2019, 16:50
Hello, how my program can recognise if I double clicked selected button?

d_stranz
18th October 2019, 23:40
You can solve this problem in at least these three ways:

- implement and install an event filter (QObject::installEventFilter()) on the QPushButton instance and look for QWidget::mouseDoubleClickEvent() events

- derive a new class from QPushButton and override the mouseDoubleClick() event

- implement slots for QAbstractButton::pressed() and QAbstractButton::released() signals. In the pressed slot, you will start a single-shot QTimer with a timeout set to QApplication::doubleClickInterval(). You will also set a counter to count the number of presses. In the released slot, you will check to see if the timer is still running and check the number of presses count. If the count is 2 and the timer is still running, it means the user has clicked the mouse twice, fast enough to be a double click. If the timeout fires before two released() signals are received, then that means either a click or two clicks depending on the count. If your released slot does detect a double click, set the counter to zero and stop the timer so you can get ready for the next double click. In the timeout slot, you should also set the counter to zero.

In any case, your button will still emit clicked(), pressed(), released(), and toggled() signals, so if you want to ignore single clicks, you should not connect slots to clicked() or toggled().

There is a reason why there is no double-clicked signal for push buttons - it is not normal behavior and goes against most user interface guidelines. So if you implement it, your users will be confused when they click a button once (as is the normal behavior) and nothing happens. They will not be expecting to have to double-click the button to get a response.

Bedopies
19th October 2019, 10:34
Selected button I meant Selected button by user ("W" or "S" or LeftMouseButton) out of GUI / in other app. I heard I can do it using QTimer, but I dont know how.

d_stranz
19th October 2019, 16:25
Selected button I meant Selected button by user ("W" or "S" or LeftMouseButton) out of GUI / in other app.

I am sorry, but I have no idea what you mean. Maybe you can explain in more than one sentence?

Bedopies
19th October 2019, 19:12
I want to do an app, what can alert / output / notify user when he double click keyboard button or mouse button.

Notification is simple, but I dont know how I can catch double click.