Results 1 to 9 of 9

Thread: QProcess - read from stdout lively

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default QProcess - read from stdout lively

    Hi, I am using using the following code to successfully collect the stout of the program I run with QProcess.

    Qt Code:
    1. QProcess *Process = new QProcess;
    2. Process->start(program, args);
    3.  
    4. Process->waitForFinished();
    5.  
    6. QTextStream rsyncStdoutStream(rsyncProcess->readAllStandardOutput());
    7. while (true)
    8. {
    9. QString line = rsyncStdoutStream.readLine();
    10. if (line.isNull())
    11. break;
    12. else
    13. newFiles.append(line);
    14. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that with this code the output is actually collected once the program is finished, so I do not have "live" access to it, which is actually what I am looking for (I would need this for other purpose). Simply not using the waitForFinished() function makes my program to crash.

    i thought that using the QProcess::readyReadStandardOutput signal could be a solution, but AFAIK this would need to call a SLOT and with this it is not possible to return any value, which is my need. So I am a bit stuck at this point..

    Thanks,
    Jan

  2. #2
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: QProcess - read from stdout lively

    Ok, I investigated it further and I have now the following code.

    Qt Code:
    1. QProgressDialog *progress = new QProgressDialog("Processing...", "Abort", 0, INT_MAX, this);
    2. progress->setWindowModality(Qt::WindowModal);
    3. progress->setLabelText("Calculating..");
    4. progress->setMinimumSize(400, 40);
    5. progress->setRange(0, 100);
    6. progress->setValue(1); // AFAIU this is what shows the progress dialog
    7.  
    8. QProcess *rsyncProcess = new QProcess;
    9. rsyncProcess->start(program, args);
    10. rsyncProcess->waitForFinished();
    11.  
    12. progress->setValue(100); // AFAIU this is where the progress dialog closes
    To copy to clipboard, switch view to plain text mode 

    The problem with this is that the dialog seems to be actually shown _after_ the rsyncprocess is finished and so it always appears only for few milliseconds, no matter the time the rsyncProcess actually takes, which is not what I need of course. I have also tried using a busy indicator with range 0, 0 but this does not change the described behavior, the dialog seems to appear after te rsyncprocess is finished.

    Qt Code:
    1. QProgressDialog *progress = new QProgressDialog("Processing...", "Abort", 0, INT_MAX, this);
    2. progress->setWindowModality(Qt::WindowModal);
    3. progress->setLabelText("Calculating..");
    4. progress->setMinimumSize(400, 40);
    5. progress->setRange(0, 0);
    6. progress->setValue(1); // AFAIU this is what shows the progress dialog
    7.  
    8. QProcess *rsyncProcess = new QProcess;
    9. rsyncProcess->start(program, args);
    10. rsyncProcess->waitForFinished();
    11.  
    12. progress->setValue(0); // AFAIU this is where the progress dialog closes
    To copy to clipboard, switch view to plain text mode 


    Any suggestion please?

  3. #3
    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 - read from stdout lively

    You are doing it wrong.
    Try something like this:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. process = new QProcess(this); // create on the heap, so it doesn't go out of scope
    5. connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput())); // connect process signals with your code
    6. connect (process, SIGNAL(readyReadStandardError()), this, SLOT(processOutput())); // same here
    7. process->start(program, args); // start the process
    8. }
    9.  
    10.  
    11. // this gets called whenever the process has something to say...
    12. void MainWindow::processOutput()
    13. {
    14. qDebug() << process->readAllStandardOutput(); // read normal output
    15. qDebug() << process->readAllStandardError(); // read error channel
    16. }
    To copy to clipboard, switch view to plain text mode 

    Success

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QProcess - read from stdout lively

    Be mindful that, depending on how the child program manages its output, you may get partial lines in processOutput() and you may need to cope with that.

  5. #5
    Join Date
    Apr 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QProcess - read from stdout lively

    how do i wrap the codes from line 4 to 7 and bind with `processOutput`?

    Quote Originally Posted by boudie View Post
    You are doing it wrong.
    Try something like this:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. process = new QProcess(this); // create on the heap, so it doesn't go out of scope
    5. connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput())); // connect process signals with your code
    6. connect (process, SIGNAL(readyReadStandardError()), this, SLOT(processOutput())); // same here
    7. process->start(program, args); // start the process
    8. }
    9.  
    10.  
    11. // this gets called whenever the process has something to say...
    12. void MainWindow::processOutput()
    13. {
    14. qDebug() << process->readAllStandardOutput(); // read normal output
    15. qDebug() << process->readAllStandardError(); // read error channel
    16. }
    To copy to clipboard, switch view to plain text mode 

    Success
    how do i wrap the codes from line 4 to 7 and bind with `processOutput`?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QProcess - read from stdout lively

    Lines 5 and 6 specifically connect the process output notifier signals to the slot.

Similar Threads

  1. Using the Stdout reading in QProcess
    By DiegoTc in forum Qt Programming
    Replies: 7
    Last Post: 29th December 2010, 14:34
  2. QProcess can read stderr but no stdout
    By mastupristi in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2010, 09:47
  3. Read most recent output from QProcess's stdout
    By Lawand in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2010, 22:29
  4. read stdout with QProcess under Windows
    By jlbrd in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2006, 17:29
  5. Cannot get stdout from QProcess
    By johnny_sparx in forum Qt Programming
    Replies: 11
    Last Post: 3rd March 2006, 11:46

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.