Results 1 to 3 of 3

Thread: QProcess wo event loop

  1. #1
    Join Date
    Oct 2008
    Location
    Budapest, Hungary
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QProcess wo event loop

    I wish to start an external process inside my gui application (QT v4.4.2, Win), the application can be suspended until the termination of that process. So far I used successfully the following code snippet:

    Qt Code:
    1. QProcess process;
    2. process.start("something", args);
    3.  
    4. if (!process.waitForStarted()){
    5. // some error msg dialog
    6. return;
    7. }
    8.  
    9. if (!process.waitForFinished(-1))
    10. return;
    To copy to clipboard, switch view to plain text mode 

    Then I could read the the complete output/error channel of the terminated process. However I wish I could process the output channels line by line during the operation of the process, because I want to feed a progress bar. Therefore I replaced the waitForFinished block to something like that:

    Qt Code:
    1. while (process.state()==QProcess::Running /* || process.waitForReadyRead(1) */){
    2. // reading process output channels
    3. }
    To copy to clipboard, switch view to plain text mode 

    My problem is that this while loop never ends, the process state always remains in Running state. How can I achieve my aim without asynchronous usage?

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QProcess wo event loop

    Have you tried a connection to this signal:
    QProcess::readyReadStandardOutput ()

  3. #3
    Join Date
    Oct 2008
    Location
    Budapest, Hungary
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess wo event loop

    Thanks for your reply but I wish to avoid the asynchronous process calling due to the nature of my task. Finally I think I have found the solution, process.waitForReadyRead must be used:

    Qt Code:
    1. process.start("something", args);
    2.  
    3. if (!process.waitForStarted()){
    4. // some error message
    5. return;
    6. }
    7.  
    8. while (process.waitForReadyRead(-1)){
    9. QByteArray newData = process.readAllStandardOutput();
    10. result = QString::fromLocal8Bit(newData);
    11. qDebug(qPrintable(QString("waitForReadyRead:")+result));
    12. }
    13.  
    14. // here the process is already finished
    To copy to clipboard, switch view to plain text mode 
    And I do not need the process.waitForFinished method at all.

Similar Threads

  1. Replies: 0
    Last Post: 23rd October 2008, 12:43
  2. Qt plug-in for GLib event loop based application
    By profoX in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2008, 14:27
  3. Glib event loop
    By Brandybuck in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2006, 17:19
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  5. Replies: 4
    Last Post: 23rd January 2006, 16:51

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
  •  
Qt is a trademark of The Qt Company.