Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QShortCut usage #13

Open
mllapps opened this issue Jan 24, 2018 · 2 comments
Open

QShortCut usage #13

mllapps opened this issue Jan 24, 2018 · 2 comments

Comments

@mllapps
Copy link

mllapps commented Jan 24, 2018

I tried this project and it is very nice. But now I want to cancel (stop) the spinner if the user push the ESC button on their keyboard.

This is only possible if I set ' disableParentWhenSpinning=false'. I want to register a QShortCut if spinner is active. Do you have any idea?

@mllapps
Copy link
Author

mllapps commented Jan 24, 2018

I tried this at initialize() method

    /* Push ESC button to abort the active request */
    auto shortcut = new QShortcut(Qt::Key_Escape, this);
    shortcut->setContext(Qt::ApplicationShortcut);
    connect(shortcut, &QShortcut::activated, [this](){
        qDebug() << "Try to cancle the active request";
    });

@hbtalha
Copy link

hbtalha commented Jul 29, 2021

For 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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants