PDA

View Full Version : start new command after finishing previous command in QProcess



alak
22nd February 2011, 13:23
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.

mcosta
22nd February 2011, 13:30
are you sure to write right?

can you post the code of your slot?

alak
23rd February 2011, 06:47
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.

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)
{
QString tmp = QString("%1 %2 %3.");
switch (exitStat)
{
case QProcess::NormalExit:
print2Output(tmp.arg(comp, "exited normaly with ", QString::number(exitCode)));
break;
case QProcess::CrashExit:
print2Output(tmp.arg(comp, "crashed with ", QString::number(exitCode)));
break;
}
}

void MainWindow::handleStarted(QString comp)
{
print2Output(comp + " was started");
}

mcosta
23rd February 2011, 08:50
Thi is your 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)