Page 1 of 2 12 LastLast
Results 1 to 20 of 33

Thread: why can't QProcess read all output?

  1. #1
    Join Date
    May 2010
    Posts
    42

    Question why can't QProcess read all output?

    hi guys!
    I'm using QProcess start a program,but I'm come across a problem.the problem is when I read the QProcess,it doesn't read all of the output.eg,actually,the output should be two lines,but it shows only one line.what's wrong with it?If someone can help me?thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: why can't QProcess read all output?

    There are two possibilities:
    1. the other line doesn't end with a \n so it wasn't flushed by the system into the output stream
    2. the other line was sent to stderr and not stdout
    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
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by wysota View Post
    There are two possibilities:
    1. the other line doesn't end with a \n so it wasn't flushed by the system into the output stream
    2. the other line was sent to stderr and not stdout
    I have knew that the second line end with a \n.and it should be the second possibility,but,before I start the program,I set the process like this: inprocess.setReadChannelMode(QProcess::MergedChann els);
    and in the connect,I write like this: connect(&inprocess,SIGNAL(readyReadStandardOutput( )),this,SLOT(iupdateTextEdit()));
    connect(&inprocess,SIGNAL(readyReadStandardError() ),this,SLOT(iupdateTextEdit()));
    the following show the iupdateTextEdit() fuction
    void mainWindow::iupdateTextEdit()
    {
    QByteArray newData=inprocess.readAllStandardOutput();
    QString text=textEdit->toPlainText()+QString::fromLocal8Bit(newData);
    textEdit->setPlainText(text);
    }
    even so,it can't show the second line in the textEdit.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: why can't QProcess read all output?

    Did you try simply readAll()?
    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
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why can't QProcess read all output?

    Disclaimer: I didn't use this... so i might be wrong, but
    as far as i know the '\n' line end doesn't flush the stream, try to use std::endl to finish the line.

  6. #6
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by wysota View Post
    Did you try simply readAll()?
    it doesn't work too.

  7. #7
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by Zlatomir View Post
    Disclaimer: I didn't use this... so i might be wrong, but
    as far as i know the '\n' line end doesn't flush the stream, try to use std::endl to finish the line.
    the program that i want to call was wrote in C not C++.

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why can't QProcess read all output?

    In C you can use:
    Qt Code:
    1. printf(...);
    2. fflush(stdout);
    To copy to clipboard, switch view to plain text mode 
    to flush the stdout buffer

  9. #9
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by Zlatomir View Post
    In C you can use:
    Qt Code:
    1. printf(...);
    2. fflush(stdout);
    To copy to clipboard, switch view to plain text mode 
    to flush the stdout buffer
    if like the following ?
    void mainWindow::iupdateTextEdit()
    {
    QByteArray newData=inprocess.readAllStandardOutput();
    QString text=textEdit->toPlainText()+QString::fromLocal8Bit(newData);
    textEdit->setPlainText(text);
    fflush(stdout);
    }

  10. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why can't QProcess read all output?

    No, don't use that in Qt.

    Use that in your C application (if you have the source of the C application)

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why can't QProcess read all output?

    If you run your C program from a dos command line, do you get all output?

  12. #12
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by Zlatomir View Post
    No, don't use that in Qt.

    Use that in your C application (if you have the source of the C application)
    unfortunately,i haven't the soures of the C application.

  13. #13
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by fatjuicymole View Post
    If you run your C program from a dos command line, do you get all output?
    If i set the Konsole to super Konsole(su -) ,i can get all output.but if i set it to "/bin/bash",it can't .

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: why can't QProcess read all output?

    Quote Originally Posted by Zlatomir View Post
    Disclaimer: I didn't use this... so i might be wrong, but
    as far as i know the '\n' line end doesn't flush the stream, try to use std::endl to finish the line.
    Flushing the stream is done by the runtime (or even the operating system), not by the application hence there is no difference between \n and std::endl. And I'm sure \n causes the stream flush, at least in those systems that I know of.
    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.


  15. #15
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by Raul View Post
    hi guys!
    I'm using QProcess start a program,but I'm come across a problem.the problem is when I read the QProcess,it doesn't read all of the output.eg,actually,the output should be two lines,but it shows only one line.what's wrong with it?If someone can help me?thanks!
    to my suprise,it sometimes can read the second line.

  16. #16
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why can't QProcess read all output?

    @Wysota: You are right, the actual flush is "done" by the OS, but "when" is done can be controlled from your code, and the new-line character '\n' doesn't "force" the OS to flush the output stream, while the endl does.

    @Raul: your problem is within the C application
    i don't think that you can force the stream to flush from QProcess (i might be wrong) maybe you can wait for the C application to finnish (that must cause a flush) and then see what you have in that byte-array (again i don't know if QProcess still "capture" the flush if it is closing the other process)

  17. #17
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by Raul View Post
    If i set the Konsole to super Konsole(su -) ,i can get all output.but if i set it to "/bin/bash",it can't .
    Then it seems like you can't get the same behaviour from your Qt app without first requesting su?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: why can't QProcess read all output?

    Quote Originally Posted by Zlatomir View Post
    @Wysota: You are right, the actual flush is "done" by the OS, but "when" is done can be controlled from your code, and the new-line character '\n' doesn't "force" the OS to flush the output stream, while the endl does.
    I don't know if this is the case in all basic operating systems but at least in Unices the default buffering for streams is "line buffering", so \n has to force a flush. Anyway take a look at any decent C application and check if every printf() is followed by fflush.

    i don't think that you can force the stream to flush from QProcess (i might be wrong)
    You are not wrong. What one could do is to use stddup, disable buffering of the newly created stream and set that stream as stdout of the newly created process (of course you need to spawn it yourself using proper native means). At least in Unices

    Anyway the "problem" is obviously in behaviour of the slave program.
    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.


  19. #19
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why can't QProcess read all output?

    @Wysota: I didn't said you are wrong, and you are right about line-buffer in *nix, but this isn't a behavior that you can assume it's true (especially if you want multi-platform)
    Anyway take a look at any decent C application and check if every printf() is followed by fflush.
    Correct again, it is highly not recommended to force flush on every printf()/cout, because the OS "usually" does a good job and flush the buffer whenever necessary, but this isn't a standard cross-platform "thing" (in some situations you can have different behaviors on same platform), so in some cases like the one we are talking wright now it is (or should be) necessary to "force" the flush.

  20. #20
    Join Date
    May 2010
    Posts
    42

    Default Re: why can't QProcess read all output?

    Quote Originally Posted by fatjuicymole View Post
    Then it seems like you can't get the same behaviour from your Qt app without first requesting su?
    I just use QProcess start the program.and read the process.I didn't know how to request su first.if you can tell me how to request su first.
    Last edited by Raul; 6th June 2010 at 12:50.

Similar Threads

  1. Can't get QProcess output
    By croscato in forum Qt Programming
    Replies: 6
    Last Post: 18th November 2010, 15:56
  2. Problem with QProcess and output
    By mmm286 in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 16:42
  3. Help on QProcess - Output Read
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2009, 11:54
  4. QProcess and capturing output
    By Aragorn in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2007, 16:57
  5. QProcess output in TreeView?!?
    By nupul in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2006, 08:05

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.