Results 1 to 9 of 9

Thread: QProcess - read from stdout lively

  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
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: QProcess - read from stdout lively

    Thanks boudie, this clarify a bit about how I should do with the QProcess object. However I do not understand the benefit to have it in the heap so that it does not go out of the scope.. Just to clarify it to myself, having it in the heap is not enough, I also need to have it defined as class member in order to be available also for other functions of the class. Do I get it right?

    Also, in my program now I have
    Qt Code:
    1. 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);
    7.  
    8.  
    9. rsyncProcess = new QProcess(this);
    10. connect(rsyncProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(showProgress()));
    11. rsyncProcess->start(program, args);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void SyncHandler::showProgress()
    2. {
    3. static int x = 1;
    4.  
    5. QStringList newFiles;
    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. progress->setValue(++x);
    15. //qDebug() << x;
    16. }
    17. }
    18. qDebug() << x;
    19.  
    20.  
    21. } }
    To copy to clipboard, switch view to plain text mode 

    In this way it works as long as x is actually increased exactly to 100, the value set with setRange(0,100). If x is lower than 100 the progress dialog just stay there waiting for the maximum value in the range, if higher a new one progress dialog is created.


    Added after 48 minutes:


    I have another problem with this. I actuially need to wait for process to finish before continuing as the next part of my code is based on the result of this process. So if I remove rsyncProcess->waitForFinished(); is a problem, my application goes ahead without having the needed information as process is running somehow in background and it is not finished yet.
    Last edited by dotjan; 22nd February 2012 at 18:06.

  6. #6
    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

    Add this connection:
    Qt Code:
    1. connect(rsyncProcess, SIGNAL(finished()), this, SLOT(processFinished()));
    To copy to clipboard, switch view to plain text mode 
    and create a function processFinished() where you do what has to be done.

    You should do yourself a favour and read the docs. It's really a very good investment of your time (and ours...).

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

    Default Re: QProcess - read from stdout lively

    Hi, finally I could figure it out. I was completely missing the logic and so some pieces in the code, not the function itself which of course I had already come across.... BTW, sometimes the problem is not about not reading docs but fully understanding it, I think that's we novices ask this basic things to you experts.... Thanks for your time anyway...

  8. #8
    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`?

  9. #9
    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.