QProcess startDetached can open the exe but start cannot
-
Qt5.7
mingw5.3 32bitsstartDetached work
bool const can_open = QProcess::startDetached("\"" + app_dir_path + "/auto_updater/auto_updater\"", QStringList()<<"-n"); qDebug()<<"app can open : "<<can_open;
But start do not, error type is failed to open
process.start("\"" + app_dir_path + "/auto_updater/auto_updater\"", QStringList()<<"-n", QIODevice::ReadOnly); if(process.waitForFinished(-1)){ //....do something }else{ //will reach here qDebug()<<"cannot wait for finished : "<<process.state(); }
I use QFile to check the exe exist or not, the exe could be opened
{ QFile file(app_dir_path + "/auto_updater/auto_updater.exe"); file.open(QIODevice::ReadOnly); qDebug()<<"auto updater can open : "<<file.isOpen(); }
The path are absolute path
Do anyone know what is happening?How could I fix the issues?Thanks
-
@tham
have you tried creating object ?QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments);
http://doc.qt.io/qt-5/qprocess.html#details
http://stackoverflow.com/questions/18074826/qprocess-fails-to-execute-external-executable
https://forum.qt.io/topic/71296/qprocess-is-not-working-in-linux/3 -
@Ratzz said in QProcess startDetached can open the exe but start cannot:
have you tried creating object ?
Yes, I tried to put it on heap before(for reading error message because object on stack will die after it leave the scope), but this cannot work either.
I try to search the solution by google, change it to native path, add """, use absolute path, but none of them work.
-
Thanks, I tried as you suggest, but it cannot work either.
process.start((app_dir_path + "/auto_updater/auto_updater"), QStringList()<<"-n", QIODevice::ReadOnly); if(process.waitForStarted(-1) && process.waitForFinished(-1)){ //the process never start QString const process_output(process.readAll()); qDebug()<<"process output : "<<process_output; if(process_output.contains("need to update")){ return true; }else{ return false; } }else{ //still come here qDebug()<<"cannot wait for finished : "<<process.state(); }
I tried to allocated the memory on heap and connect the errorOccured signal, the error message is "QProcess::FailedToStart"
-
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 ?