QProcess waitForFinish exits immediately
-
Hello!
I have a script.py which i want to run with 2 arguments, this scripts after launching takes about 40 secs, then prints data tto standard output. The problem i am having is that waitForFinish doesn't wait till this script end but instead fires immediately and i cannot capture stdout. Is my code missing something?QSharedPointer<QProcess> process = new QProcess(); QString execute_cmd = "script.py"; process->start(execute_cmd, QStringList() << "-y" << "argg"); process->waitForFinished(-1); if(process->exitStatus() == QProcess::ExitStatus::NormalExit) { QString result = process->readAllStandardOutput(); // do sth }
-
@Kyeiv
OK, then at least executing a.py
should be possible, provided you havechmod +x
-ed it. However, if you are default Ubuntu then.
is not on yourPATH
, so I don't know where you think it's going to pick up thescript.py
from. Do you really execute that in abash
shell by just typingscript.py
, is it on yourPATH
?For errors, you have
if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
why would you not look at the possible return results other than
NormalExit
and report them?Use
process->readAllStandardError()
at least,bash
may be chatting to you there. There is alsoerrorOccurred
signal. -
@Kyeiv
No idea, should work. Try runningpython
/python3
as the command instead of relying on file association. Don't just check for "NormalExit", always check for errors, especially if you are having an issue.QProcess::waitForFinished()
can be dodgy, especially if under Windows (and if you are under Windows how do you expect executing a.py
file to work?), and for this question I'm surprised you don't think it's relevant to say what OS you are on. Consider whetherQProcess::execute()
might be an improvement onwaitForFinished()
anyway, or test it with proper signals & slots. Also test with a different Python script than whatever yours is. All of these are standard debugging techniques. -
-
@Kyeiv
OK, then at least executing a.py
should be possible, provided you havechmod +x
-ed it. However, if you are default Ubuntu then.
is not on yourPATH
, so I don't know where you think it's going to pick up thescript.py
from. Do you really execute that in abash
shell by just typingscript.py
, is it on yourPATH
?For errors, you have
if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
why would you not look at the possible return results other than
NormalExit
and report them?Use
process->readAllStandardError()
at least,bash
may be chatting to you there. There is alsoerrorOccurred
signal. -
@Kyeiv
OK, but please put the error checking code in anyway. With OS commands you never know what might go wrong (just like you discovered), so it's important to check for & report all errors! And especially if you are going to be distributing this....