Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 have chmod +x-ed it. However, if you are default Ubuntu then . is not on your PATH, so I don't know where you think it's going to pick up the script.py from. Do you really execute that in a bash shell by just typing script.py, is it on your PATH?

    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 also errorOccurred signal.



  • @Kyeiv
    No idea, should work. Try running python/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 whether QProcess::execute() might be an improvement on waitForFinished() 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.



  • sorry for not mentioning, i am using Ubuntu 18.04.
    how to correctly catch errors in this case?


  • Lifetime Qt Champion



  • @Kyeiv
    OK, then at least executing a .py should be possible, provided you have chmod +x-ed it. However, if you are default Ubuntu then . is not on your PATH, so I don't know where you think it's going to pick up the script.py from. Do you really execute that in a bash shell by just typing script.py, is it on your PATH?

    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 also errorOccurred signal.



  • @JonB chmod +x solved issue, thank you for help! Closing.



  • @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....