Important: Please read the Qt Code of Conduct -

Enable a button after it has been disabled

  • Hi, I have more than 60 pushbuttons in my project. For each of the buttons, if the user pressed it for 5seconds or more than the button is disabled. Then if the user presses that same button for 5 seconds, the button is enabled.
    So far I have achieved the disabled part, but I am having trouble in enabling the disabled button.
    I have used two timers, one for enabling and the other for disabling the button.

    My code is:

     timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(disable_button()) );
        timer1 = new QTimer(this);
        connect(timer1,SIGNAL(timeout()), this, SLOT(enable_button()) );
    void MainWindow::on_Btn_clicked(bool checked)
        if(checked == true)
            qDebug() << 1;
        if(checked == false)
            qDebug() << 0;
    void MainWindow::disable_button()
    void MainWindow::enable_button()
        if (ui->Btn->setDisabled(true))

  • Lifetime Qt Champion

    When you disable a widget. it will not get any events. Or at least not click ones.

    So when a button is disabled it means that it should not emit a signal if clicked ?
    and to unlock it, user should hold for 5 secs?

    I think this would be easier with a custom widget that internally would keep track of locked/unlocked status by long press.

    If unlocked and user just click and release fast. will it then call its function ?
    So it can hold to lock/unlock and if unlocked, it will call its clicked slot ?

  • What @mrjj said. You can't press a disabled button, that's the whole point of disabling it.

Log in to reply