Important: Please read the Qt Code of Conduct -

[SOLVED]Shell error output from QProcess

  • How do you get the error message given by the shell if the command given to QProcess's start() method was invalid? E.g. on Linux, the shell returns a message "X: command not found". From what I can tell, the finished signal is not emitted in this case so you can't check that way. Also, calling readAllStandardOutput() and readAllStandardError() after waiting for the process to finish return empty if the command was invalid. How do you check for this case?

  • If you are on windows??

    In your pro file add:
    CONFIG += console

    And you start your program using the dos prompt from the Qt installation via Start...

  • My apologies, apparently I wasn't clear.
    I have a Qt project compiling and running O.K., the problem is it contains code to launch an external program using a QProcess object. The program string passed to the QProcess object's start() method is loaded dynamically and can be changed by the user. If the user has mistyped the string, or the command they have entered is invalid, I would like to give the user feedback by displaying the message normally returned by the shell (i.e. if you were to enter the command manually into the shell). Does that make sense?
    P.S. No I'm on Ubuntu

  • But you do not use the shell, so where should the error come from?

    You are using the system API for process start, which would be on Windows CreateProcess, whatever it is on Linux. This is different to the shell.

    Did you try to use the method "QProcess::errer": to get error information?

  • I have a similar application. I use :


    [ interleaves the standard output and standard error of the running process. then reading standard output reads everything. Have a look at enum QProcess::ProcessChannelMode for other options.]

    @connect SIGNAL( readyReadStandardOutput() )@

    to a slot and print the output of readAllStandardOutput() in the slot.

    I am able to catch both error and normal output from the console program.

    But if the program name is misspelled, then enum ProcessError from function error() might help you.

    [EDIT: code formatting, Volker]

  • Like Gerolf said, you are not using a shell, but the QProcess API. It's the error handler of your shell that is printing "X: command not found".

    With QProcess, you can achieve the same goal by reading ProcessError :

    connect(process, SIGNAL(error(QProcess::ProcessError)),
    this, SLOT(slotProcessError(QProcess::ProcessError)));

    void YourClass::slotProcessError(QProcess::ProcessError error)
    switch (error) {
    case QProcess::FailedToStart :
    errorPTE->appendPlainText(trUtf8("> The process failed to start. Either the invoked
    program is missing, or you may have insufficient
    permissions to invoke the program."));

  • Thanks for the clarification about the shell etc. I connected a slot to the QProcess::error() signal and it's working great.

Log in to reply