Long press button with progress dialog
-
Dear all,
I implemented a long press button. So if the user holds pressed for less than 2 sec. nothing happens. Holding for more than 2sec. will trigger an action on timer timeout. So far so good.
Now I want to have progress dialog showing the growing bar before the 2sec. happen.
The problem is that when i implement the dialog, for some reason, my long press button looses focus and Qt automatically throws a release event on it. Which breaks the functionality i intend to implement.
Is there any way to prevent Qt from throwing that release event?
Or how would you implement this simple functionality?
Here is my current code:
m_longClickTimer.setSingleShot(true); connect(ui.button_auto, &QPushButton::pressed, this, &MainWindow::StartLongPress); connect(ui.button_auto, &QPushButton::released, this, &MainWindow::CheckLongPress); connect(&m_longClickTimer, &QTimer::timeout, this, &MainWindow::LongPressTimeOut); void MainWindow::StartLongPress() { m_longClickTimer.start(2000); m_progressdialog_auto = new QProgressDialog(ui.button_auto); m_progressdialog_auto->setModal(false); m_progressdialog_auto->show(); } // BUG HERE // this method is triggered automatically if i open a diglog void MainWindow::CheckLongPress() { if (m_longClickTimer.isActive()) { qDebug() << "CheckLongPress: Released too early"; m_longClickTimer.stop(); m_progressdialog_auto->deleteLater(); } } void MainWindow::LongPressTimeOut() { qDebug() << "LongPress: My next action here"; }
-
@wasawi2 said in Long press button with progress dialog:
Any other ideas will be appreciated.
Try
Qt::WindowDoesNotAcceptFocus
window flag on your progressBar widget.
OrQt::Tooltip
. AFAIK tooltips dont steal focus. Might be a bit hacky. Would prefer the first one if it works.Edit:
There is also thissetAttribute(Qt::WA_ShowWithoutActivating)
which might be the best solution if it does what you expect
-
@wasawi2
To be clear: you start a key press on a focussed button. That open a modeless dialog and shows it. Doubtless that causes some kind of focus change and that causes button-press-termination in the original window, with a release event. Right? I don't know but either this has to happen or maybe you can do something about the new dialog now getting/accepting focus so that does not occur?P.S.
Try your code with a plainQDialog
instead of aQProgressDialog
, and also with a plainQWidget
. Any difference? Then we know whether it is anything to do with being aQProgressDialog
. -
@JonB Thank you very much.
Yes, what you explain is exactly what happens. I have tried with QDialog, QMainWindow and QProgressDialog, all behave in the same way, the release event is triggered when a new window opens...
So it seems I will have to find a workaround. I'm quite surprised because this kind of functionality is not too rare.
Any other ideas will be appreciated.
-
@wasawi2 said in Long press button with progress dialog:
Any other ideas will be appreciated.
Try
Qt::WindowDoesNotAcceptFocus
window flag on your progressBar widget.
OrQt::Tooltip
. AFAIK tooltips dont steal focus. Might be a bit hacky. Would prefer the first one if it works.Edit:
There is also thissetAttribute(Qt::WA_ShowWithoutActivating)
which might be the best solution if it does what you expect
-
I would use something not that intrusive. Maybe color the button over time to show the progress.
-
@Pl45m4 said in Long press button with progress dialog:
setAttribute(Qt::WA_ShowWithoutActivating)
@Pl45m4 Thank you so much! that made the trick!
@Christian-Ehrlicher Thank you for your suggestion. Unfortunately in my case i use a touch screen and the buttons have to be the size of a finger, so i can't display anything in the button itself.
-
-
@wasawi2 said in Long press button with progress dialog:
buttons have to be the size of a finger, so i can't display anything in the button itself.
How does this prevent coloring the button?
-
@Christian-Ehrlicher the user will not see well the button because they will have the finger on top of it blocking visibility.