0

I am trying to run a script in parallel to my Qt program and am having trouble starting it as a separate process. Check out my attempts below and let me know what you see wrong.

The first attempt was just a system call:

system("python3 startprocess.py");

This works but it also stops the program while running it.

I then followed this guy https://forum.qt.io/topic/92205/writing-commands-to-linux-bash with no success. No errors, just no start of my script.

I am trying this after I saw the documentation and have the below code.

QProcess process;
process.start("python3 startprocess.py");
process.waitForStarted();

I am just wanting to start this script and have it run at the same time as my C++ code. Perhaps I am using the QProcess wrong?

UPDATE: It was a lot easier to use QThreading and the original system call.

2
  • Using QThread might be easier, but could lead to bugs. I suggest trying again with the QProcess! Commented Jan 9, 2020 at 7:14
  • As my rules of thumb on how to use QProcess without trouble: 1. Avoid all the waitForXxx methods (they cause event loop to be re-entered, which can lead to issues in complex applications, which means all GUI applications). 2. Connect all the signals of QProcess, at least to a lambda with qDebug print, so you will notice when things don't work the way you expect. 3. Pass the parameters as QStringList to avoid surprises with splitting at spaces. Commented Jan 9, 2020 at 22:54

1 Answer 1

1

I think the issue is that the QProcess doesn't have the file path and fails to find and start it! I suggest first to use the full file path! Also check the QProcess::setWorkingDirectory and QProcess::setProcessEnvironment that are useful to handle this case!


Update

In order to prevent the QProcess to be killed while running and without freezing the GUI, you need to define it as a pointer, then connect the QProcess::finished event; in the slot, you can check the exit code and delete the sender using QObject::deleteLater method. Check both the Qt example and the QProcess::finished.


Update 2

Try this code:

auto process = new QProcess(this);
connect(process, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished), 
[this](int exitCode, QProcess::ExitStatus exitStatus) 
{
    if (exitStatus == QProcess::ExitStatus::CrashExit 
        || exitCode != 0) {
        // Process error!
    } else {
        // Process OK!
    }
});
process->setWorkingDirectory("startprocess.py folder location");
process->start("python3 startprocess.py");
if (!process->waitForStarted(-1)) {
    // Failed to start process
    delete process;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok tried setworkingdirectory right above the process.start and QProcess "destroyed it while python was still running" which is odd with the waitforstarted in there. I'm trying but don't understand the setProcessEnviroment but I'll give it a shot. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.