Disabling a button while a task is executing
-
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. -
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.@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.
-
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. -
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. -
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.@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 whenfinished
is received.