Stop a waiting spinner QWidget using QShortcut
-
I have been using this QtWaitingSpinner and it works as expected.
However I want to be able to cancel it any time it is spinning using the Esc key. But using QShortcut does work because once it start spinning it sets my MainWindow disabled.
My problem is also described in this issue, that had no reply.
I would appreciate any idea on how to make QShortcut work with QtWaitingSpinner .
-
@hbatalha
I read through theQtWaitingSpinner
. You sayBut using QShortcut does work because once it start spinning it sets my MainWindow disabled.
But (I read it as) that is only an option:
As an alternative example, the code below will create a spinner that (1) blocks all user input to the main application for as long as the spinner is active, (2) automatically centres itself on its parent widget every time "start" is called and (3) makes use of the default shape, size and colour settings.
QtWaitingSpinner* spinner = new QtWaitingSpinner(this, Qt::ApplicationModal, true); spinner->start(); // starts spinning
and you yourself said
This is only possible if I set ' disableParentWhenSpinning=false'.
You are passing the constructor
false
for this, aren't you? -
I read through the QtWaitingSpinner. You say
But using QShortcut does work because once it start spinning it sets my MainWindow disabled.
Actually there's a missing
not
in that statement. It does not work.You are passing the constructor false for this, aren't you?
No, I forgot to mention that I need the MainWindow to be disabled or simply disable all its widgets.
If I pass false then the user can navigate the MainWindow, behavior that I want to avoid. And if I pass true QShortcut does not work.A plan B solution I just thought right now is to disable everything in the MainWindow, toolbar, menubar, they are not that many. It's an ugly solution though.
-
I came up with a solution that turned out not to be as ugly as I first pictured it
I replied the answer to that issueFor new comers: I came up with a solution that works for me.
First I used the fork from https://github.com/huxingyi/QtWaitingSpinner (found in the PR in this repo), that allowed me to use text. Note that it uses an obsolete (I am using Qt 6.1.2) method, I fixed that and you can find the fix in a PR I opened there.
Now when using:
I set
disableParentWhenSpinning
to false when calling the ctor, that way it permits me to use QShortcut in the MainWindow and then went to disable all the widgets I had present in my MainWindow.MainWindow::MainWindow() : QMainWindow(0) , ui(new Ui::MainWindow) { ... spinner = new WaitingSpinnerWidget(this, Qt::ApplicationModal, false); spinner->setText(tr("Press Esc to cancel")); spinner->setTextColor(Qt::black); QShortcut* sh = new QShortcut(QKeySequence(Qt::Key_Escape), this); connect(sh, &QShortcut::activated, this, [this] { setSpinner(false); }); // you might check if `spinner` is running before calling the function setSpinner ... } void MainWindow::setSpinner(bool enabled) { if(enabled) spinner->start(); else spinner->stop(); menuBar()->setDisabled(enabled); ui->toolBar->setDisabled(enabled); ui->tableWidget->setDisabled(enabled); }