Results 1 to 12 of 12

Thread: Cannot get stdout from QProcess

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Cannot get stdout from QProcess

    I am having trouble to retrieve the console output from an external program. Below, I modified the program so that it is similar to what I have in my program - but with the external program changed to "dir /w".

    Qt Code:
    1. QProcess System_Call(this);
    2. QStringList Parameters;
    3.  
    4. Parameters<<"/w";
    5.  
    6. System_Call.start("dir",Parameters);
    7. System_Call.waitForStarted();
    8. X = System_Call.readAll();
    9. System_Call.waitForFinished();
    10. Parameters.clear();
    To copy to clipboard, switch view to plain text mode 

    I cannot seem to retrieve the output from this program... what am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Cannot get stdout from QProcess

    What about readAllStandardOutput() ?
    a life without programming is like an empty bottle

  3. #3
    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: Cannot get stdout from QProcess

    I think you should read after the process has finished.

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Cannot get stdout from QProcess

    Quote Originally Posted by wysota
    I think you should read after the process has finished.
    but if app using flush(stdout) first method can be possible?
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot get stdout from QProcess

    Quote Originally Posted by zlatko
    but if app using flush(stdout) first method can be possible?
    No, because you still might try to read the data before the process has written it.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot get stdout from QProcess

    Hmm.. this works fine on Linux (Ubuntu, Qt 4.1.1, gcc 4.0.2) but not in Windows (XP+SP2, Qt4.1.1, VS2003).

    Qt Code:
    1. QProcess proc;
    2. proc.start(QString("dir"));
    3. proc.waitForFinished(-1);
    4. qDebug() << proc.readAll();
    To copy to clipboard, switch view to plain text mode 

    Edit: means that it outputs correct info in linux, and nothing in windows..

  7. #7
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot get stdout from QProcess

    Quote Originally Posted by jpn
    Hmm.. this works fine on Linux (Ubuntu, Qt 4.1.1, gcc 4.0.2) but not in Windows (XP+SP2, Qt4.1.1, VS2003).

    Qt Code:
    1. QProcess proc;
    2. proc.start(QString("dir"));
    3. proc.waitForFinished(-1);
    4. qDebug() << proc.readAll();
    To copy to clipboard, switch view to plain text mode 

    Edit: means that it outputs correct info in linux, and nothing in windows..
    Is the Qt application built as console, or are you checking the dDebug output with the debugger? Otherwise you wont see any qDebug output on windows.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot get stdout from QProcess

    Quote Originally Posted by seneca
    Is the Qt application built as console, or are you checking the dDebug output with the debugger? Otherwise you wont see any qDebug output on windows.
    With a debugger... I can see qDebug() outputs fine, that is not the problem.

  9. #9
    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: Cannot get stdout from QProcess

    Reading all output at once, even after the application is finished is not a very good idea. For short outputs it is fine, but if you expect a large amount of data to come through that pipe, you should use a signal/slot mechanism to read the data as it comes in. Why? Because it may happen that you fill the pipe buffer between those two processes and the child process will never finish, because it'll be waiting until some of the buffer is read so that it can write all the remaining data. So you should use readyReadStdout() and family.

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot get stdout from QProcess

    My point is, that I never get any output on windows with the above code.
    Even when I set the working dir so that the output is expected to be extremely short.

  11. #11
    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: Cannot get stdout from QProcess

    Quote Originally Posted by jpn
    My point is, that I never get any output on windows with the above code.
    Even when I set the working dir so that the output is expected to be extremely short.
    Because there is no "dir" executable on Windows. "dir" is a command.com (or cmd.exe) command, so you have to run it from the shell. There are threads on this forum that cover the topic, maybe you should take a look at them.

  12. The following user says thank you to wysota for this useful post:

    jpn (3rd March 2006)

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Cannot get stdout from QProcess

    Quote Originally Posted by wysota
    Because there is no "dir" executable on Windows. "dir" is a command.com (or cmd.exe) command, so you have to run it from the shell. There are threads on this forum that cover the topic, maybe you should take a look at them.
    Ahh, now I get the point..

Similar Threads

  1. 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
  2. QProcess exitStatus()
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:51
  3. Problem with qprocess
    By resal in forum Qt Programming
    Replies: 8
    Last Post: 29th August 2007, 22:13
  4. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  5. QProcess problem in accessing stdout
    By aruna in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 17:56

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.