start new command after finishing previous command in QProcess
In mainwindow.h, i have a private QProcess p1.
In mainwindow.cpp, I run a command by p1.start(cmd). Both <finished> and <started> signals have been handled. After getting <started> signal, "started" message is printed to output.
When <finished> signal was received, in some times after that i run again cmd by p1.start(cmd).
But now in output i see two "started" messages.
If i run again p1.start(cmd) after finishing it for third times, i see three "started" message in output.
I debug and find out that the function handles <started> signal, is called second times, third times, ... .
Why does it happen?
Thank you for helping.
Re: start new command after finishing previous command in QProcess
are you sure to write right?
can you post the code of your slot?
Re: start new command after finishing previous command in QProcess
Requested codes are in the below.
Note:
In UI, when i click on run menu item in menu bar, on_run_QAction_triggered() is called. Then run_QP that was defined as (QProcess run_QP) in mainwindow.h, runs CMD1 command.
For run again CMD1 after finishing it, i click again on run menu item.
print2Output function prints to output given str.
Code:
void MainWindow::on_run_QAction_triggered()
{
run_QP.start(CMD1);
connect(&run_QP,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
this,
SLOT(run_HandleFinished
(int,
QProcess::ExitStatus)));
connect(&run_QP, SIGNAL(started()), this, SLOT(run_HandleStarted()));
}
void MainWindow
::run_HandleFinished(int ecode,
QProcess::ExitStatus estat
) {
handleFinished("comp1", ecode, estat);
}
void MainWindow::run_HandleStarted()
{
handleStarted("comp1");
}
void MainWindow
::handleFinished(QString comp,
int exitCode,
QProcess::ExitStatus exitStat
) {
switch (exitStat)
{
print2Output
(tmp.
arg(comp,
"exited normaly with ",
QString::number(exitCode
)));
break;
print2Output
(tmp.
arg(comp,
"crashed with ",
QString::number(exitCode
)));
break;
}
}
void MainWindow
::handleStarted(QString comp
) {
print2Output(comp + " was started");
}
Re: start new command after finishing previous command in QProcess
Thi is your code
Quote:
Code:
void MainWindow::on_run_QAction_triggered()
{
run_QP.start(CMD1);
connect(&run_QP,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
this,
SLOT(run_HandleFinished
(int,
QProcess::ExitStatus)));
connect(&run_QP, SIGNAL(started()), this, SLOT(run_HandleStarted()));
}
You connect QProcess::started and QProcess::finished signal each time you call MainWindow::on_run_QAction_triggered.
Try Connecting signals once outside the slot (in the constructor for example)