QProcess startDetached can open the exe but start cannot
-
You shouldn't test for both waitForStared and waitForFinished in the same if, they represent different states. Your application can fail to start for different reason that it fails to finish.
Also, you should read from readAllStandardError and readAllStandardOutput.
-
@SGaist
Thanks for your suggestion, but it still cannot open the exe(original codes works perfect if I use Qt5.6.1).QProcess process; process.start((app_dir_path + "/auto_updater/auto_updater"), QStringList()<<"-n", QIODevice::ReadOnly); if(!process.waitForStarted(-1)){ //come here qDebug()<<"cannot start"; return false; } if(process.waitForFinished(-1)){ QString const process_output(process.readAllStandardOutput()); qDebug()<<"process output : "<<process_output; if(process_output.contains("need to update")){ return true; }else{ return false; } }else{ qDebug()<<"cannot wait for finished : "<<process.state(); }
ps : I test waitForStarted separately before, put it in the same if to simplify the example, sorry if this confuse you.I connect to the signal errorOccured too(in this case, I allocate QProcess on heap, memory is free or not is not a big deal since this is dirty codes for debug purpose only), the error is "QProcess::FailedToStart".
-
QProcess * process = new QProcess; QObject::connect(process, &QProcess::errorOccurred, [] (QProcess::ProcessError error) -> void { qDebug() << "Error: " << error; }); QObject::connect(process, &QProcess::started, [] () -> void { qDebug() << "Process has started!"; }); QObject::connect(process, &QProcess::finished, [process] () -> void { qDebug() << "Process has finished!"; qDebug() << process->readAllStandardOutput(); }); QObject::connect(process, &QProcess::finished, process, &QObject::deleteLater);
As a rule of thumb avoid the the
waitFor*
functions.
... and please handle the errors you might be getting. -
First of all, thanks for your helps, but this do not solve the problem.
As a rule of thumb avoid the the waitFor* functions.
I know what you mean(avoid gui freezing), but in my case, waitFor* is a right choice. Especially suit for a simple example.
... and please handle the errors you might be getting.
I do connect the signal of errorOccured as I mentioned before and write down what kind of error I get, I did not include them in the source codes because this could make the example easier to read.
-
What do you get from reading from stdout and stderr ?
-
@tham said in QProcess startDetached can open the exe but start cannot:
but in my case, waitFor* is a right choice.
Almost never is, but let's leave that discussion for some other time.
@tham said in QProcess startDetached can open the exe but start cannot:
error type is failed to open
Then the file can't be found. Write down the full path you're attempting to start, make sure it is an executable (it has
-x
for the user/group access) and you might also want to remove the quotes from the path. -
@SGaist said in QProcess startDetached can open the exe but start cannot:
What do you get from reading from stdout and stderr ?
Get nothing, because process.waitForStarted(-1) always return false(qDebug print "cannot start"), error message emit by errorOccured signal is "QProcess::FailedToStart". The weird part is
1 : startDetached can start the exe with the same path and same command(-n)
2 : start api of Qt5.6.1 work -
But why don't you print the output of
process.readAllStandardOutput
andprocess.readAllStandardError
after it failed to start ?