PDA

View Full Version : QProcess calls finished signal before started process exits



sa5webber
1st June 2012, 21:53
I'm new to QT so I'm sure I'm either misunderstanding something or just doing something wrong.
The problem is that when I run the code below, it opens up the application and also immediately
calls the finished() function even though the application is still running. I wouldn't expect it to
emit the finished signal until after the application has exited?

What am I missing?

Here is my test code.

*****************************************
mainwindow.h
==========

private slots:
void procEditFinished(int exitCode, QProcess::ExitStatus exitStatus);

private:
QProcess *procEdit;

mainwindow.cpp
============

void MainWindow::on_btnEdit_clicked()
{
QString app = "/usr/bin/gedit";
QStringList arg;

arg << "file.txt";
procEdit = new QProcess(this);
connect(procEdit, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(procEditFinished(int, QProcess::ExitStatus)));

procEdit->start(app, arg);
}

void MainWindow::procEditFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug("exitCode= %d, exitStatus= %d", exitCode, exitStatus);
procEdit->deleteLater();
}

wysota
2nd June 2012, 01:46
My guess would be that gedit forks and quits the parent process to detach itself from the terminal.

sa5webber
2nd June 2012, 05:26
Thanks for the reply. I don't think that is the problem. I see the same behavior when executing a linux shell script. I've also done the same thing with gedit using mono (linux implementation of .net) which implements a similar Process class. That class doesn't show gedit exiting until I close it.

ChrisW67
2nd June 2012, 06:34
Gedit V2+ only allows a single running instance. If there is an existing gedit instance running then the new instance you start communicates the file name to the existing instance and terminates immediatly. If there is no existing instance of gedit running then the process does not terminate until you close that instance of the editor i.e. what you were expecting. As far as I can tell there is no way to suppress this behaviour.

sa5webber
3rd June 2012, 06:45
You're right. It works if no other instance is running to receive the new file being opened so the other instance can terminate. At least understanding what's happening helps with how to handle it. Thanks.