QPushButton released() runs twice because of focus change
-
I have a widget with 4 QPushBbuttons: i have used the QAbstarctButton::pressed() event to start a timer and count for a long press, and i have used the QAbstarctButton::released() event to react to a short press, if the long press was not triggered.
Everything works, except one of the buttons' long press function is to display a non-modal QDialog: this causes the base widget to lose focus, in turn causing my button to emit an extra released() signal, causing its short press intended code to run twice: one time due to the focus change of the base widget, and the other being the actual user's finger leaving the touchscreen surface.
Is there any way to bypass this behavior without changing the structure?
Some code:
... //somewhere in main QTimer softKeypadTimer; connect(softKeypadTimer, SIGNAL(timeout()), this, SLOT(on_softKeypadTimeout()), Qt::UniqueConnection); int softKeypadKeyPressed; bool softKeypadLongpress; ... void MyWidget::on_btn_BottomRight_pressed() { softKeypadTimer.start(); softKeypadKeyPressed = BOTTOM_RIGHT_LONGPRESS; } void MyWidget::on_softKeypadTimeout() { //connected to softKeypadTimer timeout signal softKeypadLongpress = true; //block execution of shortpress routines emit softKeyboard(softKeypadKeyPressed); } void MyWidget::on_btn_BottomRight_released() { softKeypadTimer.stop(); if (!softKeypadLongpress) emit softKeyboard(BOTTOM_RIGHT); softKeypadLongpress = false; }
-
@JeKK666
You are only supposed to ignore it in the " one of the buttons' long press function is to display a non-modal QDialog" case, where (I believe) you say you will get the "genuine" release later when the user actually removes his finger or something. Whatever it takes to recognise this is a focus change from the dialog which will later result in another released from the finger. Something like that. -
@JeKK666 said in QPushButton released() runs twice because of focus change:
one time due to the focus change of the base widget
You are saying this happens when, is it when the
QDialog
gets displayed? If you don't get a better solution could you ignore thatreleased()
signal case? -
-
@JeKK666
You are only supposed to ignore it in the " one of the buttons' long press function is to display a non-modal QDialog" case, where (I believe) you say you will get the "genuine" release later when the user actually removes his finger or something. Whatever it takes to recognise this is a focus change from the dialog which will later result in another released from the finger. Something like that. -
@JonB Yes, this was the solution, in the end: my issue was that the 4 buttons have swappable functions, so i could not programmatically know which one would end up calling for the dialog to display.
So i've added a dedicated flag and i set it in the code that would make the dialog visible, to allow skipping of the unwanted event, it works :)Thanks @JonB.
-