Results 1 to 7 of 7

Thread: [Article] How to QProcess in QThread

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default [Article] How to QProcess in QThread

    Hello,

    I googled a lot and went all over the internet (and Qt documentation) trying to find a quick solution, but I had no luck, so eventually I had to figure it out by myself.
    This post is meant for this: just to give some quick reference to those are experiencing the same problem and need a fast idea of the solution.
    Furthermore I saw many threads (not here) unanswered for this question, so I'm trying to give an answer (that at least for me worked).

    I spent these days several hours trying to figure out how to use a QProcess object in a different thread without having qt complaining:
    Cannot create children for a parent that is in a different thread.
    because I was trying to create a QProcess to launch an external application from a different QThread than the main one.

    This error pops up because with
    Qt Code:
    1. MyThread::MyThread():QThread()
    2. {
    3. process=new QProcess(this); // QProcess process; in the class
    4. }
    To copy to clipboard, switch view to plain text mode 
    we are trying to tell that QProcess object's parent is our QThread subclass, but actually this QProcess object lives in the different thread we are going to launch, while MyThread object is, presumably, in the main thread. Actually different threads, Qt doesn't like this.

    Solution is quite easy, but I was so fretful that I didn't notice it.
    To get it work is just required to use the new thread as a new event loop (besides it is), and declare QProcess in the run() method instead of allocate it dinamically as a MyThread member pointer:
    Qt Code:
    1. // this is all the class is made of
    2. void MyThread::run()
    3. {
    4. connect(&p,SIGNAL(finished(int)),this,SLOT(quit()));
    5. p->start("extprogram.exe");
    6. exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    p doesn't go prematurely out of scope until the end because exec() enters event loop and prevent it from being destroyed.
    When process finishes execution, the connection calls the QThread's quit() slot that ends the thread; place a connection to the MyThread finished() signal in the main thread to know when it is over.

    It was such a simple answer, I can't believe it took so much time to get fixed...
    Maybe I am a dumb, but with this I hope to agile someone else's work.

    Of course every comment on this is welcome.
    HTH
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  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: [Article] How to QProcess in QThread

    I have one comment. Why spawn a new thread just to spawn a new process? In the situation described the thread does completely nothing - QProcess could have been created and started in the main thread without any loss of functionality. Or maybe I just don't see something...
    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
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: [Article] How to QProcess in QThread

    Yes S.Wysota, you are right: in this case QThread is useless, but this is meant to be a basis to use to build on.
    For example, I had to make a background process that had to backup all the databases of the program, so it had to call mysqldump batching through every database; in this situation QProcess and QThread seems quite suitable to me.
    On the other hands QProcess may be substituted by a custom user class that does more sophisticated code (as I did, by the way).
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  4. #4
    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: [Article] How to QProcess in QThread

    Quote Originally Posted by Raccoon29 View Post
    For example, I had to make a background process that had to backup all the databases of the program, so it had to call mysqldump batching through every database; in this situation QProcess and QThread seems quite suitable to me.
    How come? Was your thread doing something while the external process was running?
    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.


  5. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: [Article] How to QProcess in QThread

    Was your thread doing something while the external process was running?
    Well... actually it doesn't. It waits the process is over to launch the next one... maybe I could add some controls to make a progress bar...
    You make me wonder. Do you think QThread is useless here and everything could be done just using signals/slots?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. #6
    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: [Article] How to QProcess in QThread

    Quote Originally Posted by Raccoon29 View Post
    Do you think QThread is useless here and everything could be done just using signals/slots?
    Yes, it's completely useless. Adding a progress bar or something wouldn't change anything, you can (and should) still do it in the main thread. You can read my article in Qt Quarterly about Responsive GUIs to get some ideas. I don't say your article is bad, I say you should give a useful example.
    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.


  7. #7
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: [Article] How to QProcess in QThread

    Got it

    BTW: in a code comment I wrote "QProcess process; in the class", it's a typo: to be allocated it must be a pointer, so: "QProcess *process; in the class"... but I can't edit it anymore.
    Last edited by Raccoon29; 30th November 2009 at 08:29. Reason: updated contents
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

Similar Threads

  1. CPU Time & Memory Usage in QThread or QProcess
    By Davidaino in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 19:15
  2. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  3. QThread and QProcess problem
    By codebehind in forum Qt Programming
    Replies: 13
    Last Post: 7th August 2007, 08:11
  4. Replies: 4
    Last Post: 27th July 2006, 11:13
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

Tags for this Thread

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.