Reputation: 1107
I've wrote a qt quick desktop application with QtCreator and C++.
I want to start another application from my application. I searched and found these options:Qprocess, with the functions: start, startDetached and execute.
The application I want to start, is a single application, and others advised me to use the function startDetached.
I chose this option of the startDetached function:
bool startDetached ( const QString & program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0 )
Here is my code:
QProcess *process=new QProcess(this);
bool res;
QStringList argsList;
argsList.append("-start");
process->startDetached(emulauncherInstallationDirectory + "\\Emulauncher.exe",argsList,emulauncherInstallationDirectory);
res = process->waitForFinished();
delete process;
process=NULL;
return res;
but when I'm running my application , it works well sometimes, and doesn't work at all at aother times.
I've debugged it a lot of times, and saw that the function
process->waitForFinished();
returns false or true, with no obvious reason: in all the time the .exe file is in it's place, and if I'm running it from the command line, or by double click on the file, it runs well, but from my application - it runs sometimes well, and sometimes really not.
Anyone knows about any reason to it or about any solution to this strange problem?
Any answer would be appreciated.
Upvotes: 0
Views: 1798
Reputation: 79447
From the documentation:
bool QProcess::waitForFinished ( int msecs = 30000 )
Returns true if the process finished; otherwise returns false (if the operation timed out, if an error occurred, or if this QProcess is already finished).
So process->waitForFinished();
returns true if your process finished in 30 seconds, or false otherwise.
Use process->waitForFinished(-1);
if you want no timeout at all.
Upvotes: 3
Reputation: 743
Could it be a timeout issue? By default, waitForFinished()
waits 30 seconds:
bool QProcess::waitForFinished ( int msecs = 30000 )
Blocks until the process has finished and the finished() signal has been emitted, or until msecs milliseconds have passed.
Returns true if the process finished; otherwise returns false (if the operation timed out, if an error occurred, or if this QProcess is already finished).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
If msecs is -1, this function will not time out.
You should use waitForFinished(-1)
if you want to wait forever.
Upvotes: 2