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. Disabling a button while a task is executing
QtWS25 Last Chance

Disabling a button while a task is executing

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 511 Views
  • 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.
  • M Offline
    M Offline
    Mihic
    wrote on last edited by
    #1

    I'm having trouble with disabling a button while a task is executing.
    I have a button, which when pressed starts a procces using the QProcces class.
    Wanted behaviour is disabling the button until the process is finished, and then enabling the button again.
    Currently my code is this:

    ui->button->setEnabled(false);
    QApplication::processEvents(QEventLoop::AllEvents);
    process->start(program, arguments);
    process->waitForFinished();
    ui->button->setEnabled(true)
    

    This solution only gets me to a certian point.
    No matter how much the user spams the button after the inital click, the process only starts 2 times for some reason, but I only want the inital click processed, and other clicks ignored until the process is finished.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • M Mihic

      I'm having trouble with disabling a button while a task is executing.
      I have a button, which when pressed starts a procces using the QProcces class.
      Wanted behaviour is disabling the button until the process is finished, and then enabling the button again.
      Currently my code is this:

      ui->button->setEnabled(false);
      QApplication::processEvents(QEventLoop::AllEvents);
      process->start(program, arguments);
      process->waitForFinished();
      ui->button->setEnabled(true)
      

      This solution only gets me to a certian point.
      No matter how much the user spams the button after the inital click, the process only starts 2 times for some reason, but I only want the inital click processed, and other clicks ignored until the process is finished.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mihic Do not call waitForFinished(). Instead disable the button, connect https://doc.qt.io/qt-6/qprocess.html#finished signal to a slot where you enable the button and start the process.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • M Mihic

        I'm having trouble with disabling a button while a task is executing.
        I have a button, which when pressed starts a procces using the QProcces class.
        Wanted behaviour is disabling the button until the process is finished, and then enabling the button again.
        Currently my code is this:

        ui->button->setEnabled(false);
        QApplication::processEvents(QEventLoop::AllEvents);
        process->start(program, arguments);
        process->waitForFinished();
        ui->button->setEnabled(true)
        

        This solution only gets me to a certian point.
        No matter how much the user spams the button after the inital click, the process only starts 2 times for some reason, but I only want the inital click processed, and other clicks ignored until the process is finished.

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Mihic
        and remove QApplication::processEvents(QEventLoop::AllEvents); its probably the cause for the "double event"


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mihic
          wrote on last edited by
          #4

          The solution you proposed is not what I'm looking for, since the QProcess::finished signal will be emmited once the QProcess starts the task, not when the task itself is executed completely.
          Since the execution time of the task itself is much longer than the time it takes QProcess to start the task, behaviour the is generated is still the same, i.e. multiple clicks will start up multiple tasks while the first one is still runing.
          What I'm looking for is a way to track the execution of the task itself, and once it's completed, then the enabling of the button should occur.

          JonBJ 1 Reply Last reply
          0
          • M Mihic

            The solution you proposed is not what I'm looking for, since the QProcess::finished signal will be emmited once the QProcess starts the task, not when the task itself is executed completely.
            Since the execution time of the task itself is much longer than the time it takes QProcess to start the task, behaviour the is generated is still the same, i.e. multiple clicks will start up multiple tasks while the first one is still runing.
            What I'm looking for is a way to track the execution of the task itself, and once it's completed, then the enabling of the button should occur.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Mihic said in Disabling a button while a task is executing:

            since the QProcess::finished signal will be emmited once the QProcess starts the task, not when the task itself is executed

            No, that is not the case. QProcess::started is the signal emitted when the process is started. QProcess::finished is emitted when the process finishes.

            What I'm looking for is a way to track the execution of the task itself

            You cannot track what is happening inside a sub-process once it has started, you can only be notified when it finishes. If you are getting finished when you claim the subprocess is still "running" then something is going on in the subprocess, e.g. a shell command might do something with an & at the end to cause it to run without waiting for it. QProcess can only track the original PID returned when the process was created, and you would see this having exited when finished is received.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mihic
              wrote on last edited by
              #6

              I've found what the mistake I made was.
              The solution you originally proposed now works as intented.
              Thank you all for helping and teaching me.

              1 Reply Last reply
              1

              • Login

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