Results 1 to 7 of 7

Thread: Can't get QProcess output

  1. #1
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default Can't get QProcess output

    Hi all!

    I have the following code and can't figure out why I can't get the
    QProcess output:

    Qt Code:
    1. #include <QApplication>
    2. #include <QProcess>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication application(argc, argv);
    8.  
    9. QProcess proc;
    10.  
    11. proc.start("c:\\windows\\system32\\cmd.exe", QIODevice::ReadWrite);
    12.  
    13. if (proc.waitForStarted() == false) {
    14. qDebug() << "Error starting cmd.exe process";
    15. qDebug() << proc.errorString();
    16.  
    17. return (-1);
    18. }
    19.  
    20. // Show process output
    21. qDebug() << proc.readAllStandardOutput();
    22.  
    23. proc.write("dir");
    24.  
    25. qDebug() << proc.readAllStandardOutput();
    26.  
    27. proc.close();
    28.  
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

    Thanks!

  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: Can't get QProcess output

    You need a slot connected to this signal:
    void QProcess::readyReadStandardOutput ()** [signal]

  3. #3
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default Re: Can't get QProcess output

    I've changed the code to:

    Qt Code:
    1. #include <QApplication>
    2. #include <QProcess>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication application(argc, argv);
    8.  
    9. QProcess proc;
    10.  
    11. proc.start("c:\\windows\\system32\\cmd.exe", QIODevice::ReadWrite);
    12.  
    13. if (proc.waitForStarted() == false) {
    14. qDebug() << "Error starting cmd.exe process";
    15. qDebug() << proc.errorString();
    16.  
    17. return (-1);
    18. }
    19.  
    20. // Show process output
    21. proc.waitForReadyRead();
    22. qDebug() << proc.readAllStandardOutput();
    23.  
    24. proc.write("dir");
    25.  
    26. proc.waitForBytesWritten();
    27. proc.waitForReadyRead();
    28.  
    29. qDebug() << proc.readAllStandardOutput();
    30.  
    31. proc.close();
    32.  
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 

    and was able to get the this output:

    Qt Code:
    1. Starting C:\Documents and Settings\Usuario\Meus documentos\Teste\debug\Teste.exe...
    2. "Microsoft Windows XP [versÆo 5.1.2600]
    3. (C) Copyright 1985-2001 Microsoft Corp.
    4.  
    5. C:\Documents and Settings\Usuario\Meus documentos\Teste\debug>"
    6. ""
    To copy to clipboard, switch view to plain text mode 

    but the dir command output doesn't came out.

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

    Default Re: Can't get QProcess output

    Depending on what you trying to do, you may wish to pass "/c dir" to cmd.exe as a parameter, which will give you the dir output.

  5. #5
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default Re: Can't get QProcess output

    fatjuicymole

    Even with the "/c" parameter I can't get the dir output.

    My intention is to send commands to the cmd.exe process and receive it's output.

    There is a way of doing this?

    Thanks....

  6. #6
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't get QProcess output

    It looks like you are getting an incomplete output from the process... maybe calling QProcess::waitForFinished() before QProcess::readAllStandardOutput can solve the problem.

  7. #7
    Join Date
    Aug 2010
    Posts
    5
    Thanked 3 Times in 1 Post
    Platforms
    Symbian S60 Maemo/MeeGo

    Default Re: Can't get QProcess output

    Hi - I'm facing the a similar problem with Qt 4.7.1, running on Windows XP:

    QString program = "cmd.exe";
    QProcess *myProcess = new QProcess();
    myProcess->start(program);
    if (myProcess->waitForStarted(1000) == false)
    qDebug() << "Error starting external program";
    else
    qDebug() << "external program running";

    myProcess->waitForReadyRead(100);
    myProcess->waitForFinished(100);
    qDebug() << "read output" << myProcess->readAllStandardOutput();
    /* At this point I get output:
    read output "Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Users\myfolder\mysubfolder>"
    */
    myProcess->write("dir");
    myProcess->waitForBytesWritten(100);
    myProcess->waitForFinished(100);
    myProcess->waitForReadyRead(100);
    qDebug() << "read output" << myProcess->readAllStandardOutput();

    /*I get nothing here:
    read output ""
    */

    Is it actually possible to write to a QProcess on Windows? Is it mandatory to connect to the signals?

    Thanks


    Added after 8 minutes:


    I found the solution, need to make use of myProcess->write("dir \n"); and myProcess->closeWriteChannel();

    the following code works:
    QString program = "cmd.exe";
    QStringList arguments;
    arguments << "-v";

    QProcess *myProcess = new QProcess();
    //myProcess->start(program, arguments);
    myProcess->start(program);


    if (myProcess->waitForStarted(1000) == false)
    qDebug() << "Error starting external program";

    else
    qDebug() << "external program running";

    myProcess->waitForReadyRead(100);
    myProcess->waitForFinished(100);
    qDebug() << "read output" << myProcess->readAllStandardOutput();

    myProcess->write("dir \n");
    myProcess->closeWriteChannel();
    myProcess->waitForBytesWritten(100);
    myProcess->waitForReadyRead(100);
    myProcess->waitForFinished(100);
    qDebug() << "read output" << myProcess->readAllStandardOutput();
    Last edited by SeN; 18th November 2010 at 16:56.

  8. The following 3 users say thank you to SeN for this useful post:

    andre_teprom (19th September 2011), croscato (5th February 2014), teak421 (3rd August 2017)

Similar Threads

  1. Problem with QProcess and output
    By mmm286 in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 17:42
  2. Help on QProcess - Output Read
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2009, 12:54
  3. QProcess problem: compile well but output error
    By jx324 in forum Qt Programming
    Replies: 3
    Last Post: 2nd March 2008, 08:39
  4. QProcess and capturing output
    By Aragorn in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2007, 17:57
  5. QProcess output in TreeView?!?
    By nupul in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2006, 09:05

Tags for this Thread

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.