Results 1 to 7 of 7

Thread: run QProcess in a thread

  1. #1
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default run QProcess in a thread

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: run QProcess in a thread

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: run QProcess in a thread

    2 bigbill,
    take away "this", ie write
    Qt Code:
    1. ...
    2. cons = new QProcess ();
    3. ...
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: run QProcess in a thread

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: run QProcess in a thread

    Quote Originally Posted by bigbill View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: run QProcess in a thread

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: run QProcess in a thread

    Do not use the waitFor...() functions.
    Instead, use signals and slots

  8. The following user says thank you to tbscope for this useful post:

    bigbill (16th December 2010)

Similar Threads

  1. Replies: 0
    Last Post: 26th August 2010, 10:44
  2. Replies: 9
    Last Post: 28th November 2009, 20:31
  3. Replies: 16
    Last Post: 7th October 2009, 08:17
  4. Is a QProcess thread safe in Qt4?
    By Jay_D in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2009, 16:38
  5. Replies: 6
    Last Post: 29th April 2009, 18:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.