Results 1 to 7 of 7

Thread: Can't get QProcess output

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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.

Similar Threads

  1. Problem with QProcess and output
    By mmm286 in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 16:42
  2. Help on QProcess - Output Read
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2009, 11:54
  3. QProcess problem: compile well but output error
    By jx324 in forum Qt Programming
    Replies: 3
    Last Post: 2nd March 2008, 07:39
  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

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.