QProcess process; process.start("ls"); // start the "ls" command process.setReadChannel(QProcess::StandardOutput); // read from standard output process.waitForFinished(); // wait for the process to finish QByteArray output = process.readAll(); // read the output from the command
QProcess process; process.start("ls no_such_directory"); // start a command that results in an error process.setReadChannel(QProcess::StandardError); // read from standard error process.waitForFinished(); // wait for the process to finish QByteArray errorOutput = process.readAll(); // read the error outputIn this example, we are running a command that results in an error (trying to list a directory that doesn't exist). We set the read channel to standard error, wait for the process to finish, and read the error output. The QProcess class is part of the Qt Core module.