1
votes

I'm making a render job manager for blender. I have a class that builds up a queue of render jobs and then you click Start and it begins rendering one at a time with a loop. My problem is that the waitForFinished() method holds up my entire program. But I've read that you shouldn't use QThread with QProcess.

This is how the loop works.

do{        
if(myProcess->state() == QProcess::NotRunning) {

            myProcess->setProgram(blenderPath);
            myProcess->setArguments(arguments);
            myProcess->start();
            myProcess->waitForFinished(-1);

            //Get rid of current rendering job to prepare for the next job
            renderQueueList.pop_front();
        }

       }while(renderQueueList.empty() != true);

Can I use a separate thread to launch QProcess and what would be the best way to do this? I've read that you make an abstract of QThread or use signals and slots but it's so confusing, specially when I need to pass arguments to the process.

Thank you.

Edit: I want to add that the process must finish before running a new process. It has to go in order. That's why I think I need the process to run in its own thread.

1
Why not just listen to the finished() signal instead?MrEricSir
@MrEricSir you are correct.... Here is an article qtcentre.org/threads/26067-Article-How-to-QProcess-in-QThreadPavan Chandaka
I'm confused. The article shows how to make a process in a new thread but the commenters are saying this is bad. Which is it?LetTheWritersWrite
@LetTheWritersWrite the comments in the article say it's bad idea because the new thread is doing nothing other than starting the process ... bad idea or not depends on the context, otherwise, I don't see any "technical" reasons that make QProcess not usable with QThreadHazemGomaa
and yes, you can use QThread and start you QProcess in a thread worker. There is a very good example in doc.qt.io/qt-4.8/qthread.htmlHazemGomaa

1 Answers

4
votes

QProcess already executes in a different process, i.e. asynchronously with your application.

When you call waitForFinished() the application locks up until the process in QProcess finishes. You need to connect instead to the finished() and probably errorOccured() signals, and then your application will keep running while the QProcess runs in the background.

You'll need to change that loop to a check that the queue isn't empty and a new process start on the finished() signal.

If you run a QThread that runs QProcesses and does waitForFinished() you will free the main application thread indeed, but it's a pointless extra layer when QProcess is asynchronous already and you have have finished() to let you know when it's done without locking up a thread, be it the UI thread or a separate one.