Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using toggle pushButton to start/abort a process
Forum Updated to NodeBB v4.3 + New Features

Using toggle pushButton to start/abort a process

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rich_p
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      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();
      }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved