Results 1 to 8 of 8

Thread: QProcess and capturing output

  1. #1

    Default QProcess and capturing output

    Hello,

    I am relatively new to Qt, but so far I am enjoying it! I only hope someone has an idea what may be going wrong with my program...

    I am using QProcess to start a process and capture its output. I am running WIN32 console applications, and in some cases, it works fine. For example, I can capture output from ipconfig, or ping, or whatever other command similar to those that I may want to capture.

    The issue that I am having right now, however, is that not all programs are capable of being captured. I have access to another binary that if I execute by itself, I get output like I normally would. I can redirect it to a file using typical command line switches (> in this case)... but if I execute it using my program, it exits with a NormalStatus but with a Failed to Start code (added an if statement to check it in the ProcessedFinished function, not shown below), without any output. Several other binaries are the same way...

    I have verified that it is not a permission issue, or a path issue for that matter. It isn't failing to start the process, it is producing the failed to start QProcess code in the ProcessFinished funtion but not before.

    I don't know where to go from here, as I don't see anyone else having this problem if I search on the web. I am hoping someone here may be able to assist.

    The section of code that I am having an issue with is below:

    Qt Code:
    1. FlashDialog::FlashDialog(QWidget *parent):QDialog(parent)
    2. {
    3. setupUi(this);
    4. connect(okButton,SIGNAL(clicked()),this,SLOT(close()));
    5. connect(&proc,SIGNAL(readyReadStandardOutput()),this,SLOT(readFromStdout()));
    6. connect(&proc,SIGNAL(readyReadStandardError()),this,SLOT(readFromStdErr()));
    7. connect(&proc,SIGNAL(error(QProcess::ProcessError)),this,SLOT(processError(QProcess::ProcessError)));
    8. connect(&proc,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)));
    9.  
    10. }
    11. void FlashDialog::setFilePath(QString filePath)
    12. {
    13. proc.start(filePath);
    14. }
    15.  
    16. void FlashDialog::readFromStdout()
    17. {
    18. QByteArray newData=proc.readAllStandardOutput();
    19. QString text=statusView->toPlainText() + QString::fromLocal8Bit(newData);
    20. statusView->setPlainText(text);
    21.  
    22. }
    23. void FlashDialog::readFromStdErr()
    24. {
    25. QByteArray newData=proc.readAllStandardError();
    26. QString text=statusView->toPlainText() + QString::fromLocal8Bit(newData);
    27. statusView->setPlainText(text);
    28.  
    29. }
    30.  
    31. void FlashDialog::processFinished(int exitCode,QProcess::ExitStatus exitStatus)
    32. {
    33. if(exitStatus==QProcess::CrashExit)
    34. {
    35. statusView->append("Program Crashed";);
    36. }
    37. }
    38.  
    39. void FlashDialog::enableOkButton()
    40. {
    41.  
    42. }
    43. void FlashDialog::processError(QProcess::ProcessError error)
    44. {
    45. if(error==QProcess::FailedToStart)
    46. {
    47. statusView->append("Failed to start";);
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    I am under somewhat of a time crunch, so any suggestions or help are greatly appreciated!

  2. #2
    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: QProcess and capturing output

    Are you sure that these are Win32 binaries? Also check whether they don't require some DLLs.

  3. #3
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess and capturing output

    You might want to open the binaries in dependency walker to verify they are not corrupt, and to verify that they have all of the needed dependencies.

    [HTML]
    http://www.dependencywalker.com
    [/HTML]

  4. #4

    Default Re: QProcess and capturing output

    The binaries work fine, and are WIN32. They don't depend on anything as they execute on their own without issue.

    I have found that if I add 'cmd /c' to the front of them, they seem to work better, however one of them doesn't produce any output until the end of the process. Is there any way to flush QProcess so I can capture the output before the process ends?

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess and capturing output

    You could start the process with arguments and redirect its output to a file. Something like "program.exe > proc_out.txt".

    This works when using the command line, so it should work fine with QProcess ( it uses CreateProcessW on Windows ).

    Regards

  6. #6

    Default Re: QProcess and capturing output

    I thought about that too, but I don't think I'll be able to do what I want to do putting it into a file.

    Ultimately I want to provide a real-time status of the output of the executable that is being run using QProcess. This is the only executable I am having issues with (and unfortunately, one of the key executables I need to get a status from).

    If I try redirecting the output to a file in a command-line window (execute.exe > test.txt) I appear to only get text in it at the end as well... but if I don't redirect it, I get output as I should.

    I am guessing I am fighting with this executable, and likely there isn't a way for me to work around it, but before I give up, I want to make sure that there isn't something else that I can do to.

    Thanks for everyone's suggestions by the way!

  7. #7
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess and capturing output

    Hi,

    redirection is normally handled inside a shell, not in an external program.

    cu, Bernd
    --
    redir.cpp:

    Qt Code:
    1. #include <QDebug>
    2. #include <QProcess>
    3.  
    4. int main(void)
    5. {
    6. QProcess qp1, qp2;
    7.  
    8. // does not work, because i/o redirection is handled by a shell, not by QProcess or the external exe
    9. // here the string "> c:\\out1" (!) is passed as an argument to argecho
    10. qp1.start("c:\\uti\\argecho.exe", QStringList() << "> c:\\out1" << "1" << "2" << "3");
    11. qp1.waitForFinished();
    12. qDebug() << "qp1:" << qp1.readAll();
    13.  
    14. // this works (put the redirection before the external command!)
    15. qp2.start("c:\\windows\\system32\\cmd.exe", QStringList() << "/C" << "> c:\\out2" << "c:\\uti\\argecho.exe" << "1" << "2" << "3");
    16. qp2.waitForFinished();
    17. qDebug() << "qp2:" << qp2.readAll();
    18.  
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 

    argecho.c:

    Qt Code:
    1. #include <stdio.h>
    2.  
    3. int main ( int argc, char ** argv )
    4. {
    5. int i;
    6.  
    7. for ( i = 0; i < argc; i++ )
    8. printf( "%d: %s\n", i, argv[i] );
    9.  
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #8

    Default Re: QProcess and capturing output

    Thank you for the code, however I need to capture the output in real-time. The output appears on the screen in real time, and this is the only executable I am having problems with, so I know the concept of what I am trying to do works.

    Does QProcess wait for a certain character, like a new line escape character, before it acknowledges there is content to be read?

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.