Using toggle pushButton to start/abort a process



  • I am trying to use a pushbutton to launch a process and remain depressed for several seconds until the process completes. In addition, I'd like the user to be able to un-click the pushbutton to abort the process. I simulated the desired behavior of the GUI by inserting a delay loop into the pushbutton's slot function. Below is an example of the code

    @void test_Qmain::on_goTo_pushButton_toggled(bool checked)
    {
    int time_ms = 0;
    int delta_ms = 100;
    if(checked) {
    while(checked && (time_ms < 3000) ) {
    time_ms += delta_ms;
    Sleeper::msleep(delta_ms);
    QApplication::processEvents(QEventLoop::AllEvents);
    ui->textEdit->insertPlainText(QString::number(time_ms, 'f', 0));
    }
    ui->goTo_pushButton->setChecked(false);
    }
    }@

    This code outputs the delay time to a plain text box, which may be used to monitor the progress of the "process" called by the pushbutton. The intention is for the pushbutton to automatically become unchecked after the loop completes in ~3 seconds, or for the button to become unchecked by user selection.

    As expected, the pushbutton depresses and pops back up when selected. However, the "while" loop does not terminated. Based on various debug outputs, it appears the "checked" value becomes "false" only temporarily when the pushbutton is de-selected. The slot function continues to completion rather than exiting. Here's an example debug output, resulting from pressing and then de-selecting the pushbutton after ~800 ms.:
    @Checked! 100 ms
    Checked! 200 ms
    Checked! 300 ms
    Checked! 400 ms
    Checked! 500 ms
    Checked! 600 ms
    Checked! 700 ms
    Checked! 800 ms
    Not Checked. 0 ms
    Checked! 900 ms
    Checked! 1000 ms
    Checked! 1100 ms
    . .
    . .
    . .
    Checked! 2900 ms
    Checked! 3000 ms@

    Curiously the "checked" value seems to return automatically to a "true" state and the loop timer is uninterrupted rather than stopping or starting over. I tried explicitly forcing the function to "return" when checked is false, but the behavior did not change. Any ideas on how to use a toggled pushbutton to terminate this while loop?



  • Hi, it caused by your design fault. QTimer instead of sleeps should be used in your application.

    @
    void test_Qmain::on_goTo_pushButton_toggled(bool checked)
    {
    if(checked)
    startYourProcess();
    else
    stopYourProcess();
    }

    void test_Qmain::timerOut()
    {
    if (running)
    stopYourProcess();
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.