PDA

View Full Version : Question about QProcess



mp33919
16th November 2016, 19:31
I tried to use QProcess to run program B from program A. I want to know when program B finished so the program A could do something else.

In CentOS system, both program A and B are GUI programs:

in mainwindow.h:

private:
QProcess proc;
private slots:
void procFinished(int exitCode, QProcess::ExitStatus exitStatus);
void on_execBPushButton_clicked();

in mainwindow.cpp

void MainWindow::on_execBPushButton_clicked()
{
QStringList args;
args << "1";
connect(&proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procFinished(int, QProcess::ExitStatus)));
proc.start("<path>/B", args);
proc.waitForFinished();
}

void MainWindow::procFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
cout << "procFinished() is called" << endl;
}

These are the code to handle the QProcess. I found if I use proc.waitForFinish(), the program A could not refresh the GUI. I know that because after B is launched, I drag the B across A and leave the "trace" on A forever. If I delete this waitForFinsihed(), program A and B both are refreshing when they need.

Also, the "procFinished() is called" did not display after program B is terminated. Instead, the "... is called" display when program A is terminated. So, could anyone tell me what I did wrong?

Thanks in advance.

ChrisW67
16th November 2016, 19:52
Don't call the function waitForFinished() and your main program won't stop and wait for the other program to finish.

d_stranz
17th November 2016, 17:15
You are already connecting to the QProcess:finished() signal, so you will get notified when the process exits. As ChrisW67 says, no need to call QProcess::waitForFinished(). If there are things that Program A should not do while Program B is executing (like allow the "execBPushButton" to be clicked again), then you should disable it in the on_execBPushButton_clicked() slot and enable it again in the procFinished() slot.