简体   繁体   中英

Is this an appropriate time to use QThread with QProcess?

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.

QProcess already executes in a different process, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM