PDA

View Full Version : run QProcess in a thread



bigbill
15th December 2010, 00:49
Hello, I am trying to create a thread that will execute an external program.

.
.
void MyThread::run()
{
QProcess *cons;
cons = new QProcess( this );
cons->start("/home/bigbill/Desktop/praktiki/test");
.
.
.

when I compile I get the following error
QObject: Cannot create children for a parent that is in a different thread.

So I changed the parent object in the QProcess argument and I set it as MainWindow which is the name of the main class.
cons = new QProcess(MainWindow);

but I get an error: expected primary-expression before ‘)’ token

What is the mistake here?

wysota
15th December 2010, 00:52
As for the error, please search the forum before asking questions. There are probably about 50 threads within the last couple of months that deal with this issue. As for the whole concept - why are you using QProcess from within a thread in the fist place? It works asynchronously and will not block anything so a thread is neither required nor in any way beneficial (unless of course it does other things as well).

kuzulis
15th December 2010, 05:29
2 bigbill,
take away "this", ie write


...
cons = new QProcess ();
...

bigbill
15th December 2010, 10:02
Thanks
I want to use thread because I call start the QProcess by pressing a button and during the duration of the external program (which is many seconds) all the application freezes.

wysota
15th December 2010, 10:08
Thanks
I want to use thread because I call start the QProcess by pressing a button and during the duration of the external program (which is many seconds) all the application freezes.

That's not how QProcess works. If the application feeezes then you must be doing something wrong, QProcess will not block while the external process is running. Show us how you use QProcess in your freezing code.

bigbill
15th December 2010, 11:41
QProcess *cons;
cons = new QProcess( this );
cons->setWorkingDirectory("/home/bigbill/Desktop/praktiki");
cons->start("/home/bigbill/Desktop/praktiki/test");
if (!cons->waitForStarted())
return false;
if (!cons->waitForFinished())
return false;

Yes, I am sure that blocks in the waitForFinished() command.
But I want to display the results as soon as they are available without all the application being frozen.

tbscope
15th December 2010, 11:45
Do not use the waitFor...() functions.
Instead, use signals and slots