Show Menu
Cheatography

C++ (QProcess) Cheat Sheet (DRAFT) by

A cheatsheet on the C++ QProcess framework

This is a draft cheat sheet. It is a work in progress and is not finished yet.

QProcess

// Making a QProcess object
QProcess process;

// Common methods of the object and what they do
process.setStandardInputFile(QProcess::nullDevice());  // redirect stdin to /dev/null
process.setStandardOutputFile(QProcess::nullDevice()); // redirect stdout to /dev/null
process.start(executableName, argsList);               // launch the process; argsList is a QStringList where
                                                       // each entry is one discrete argument — QProcess passes
                                                       // them blindly to the executable, so the order and
                                                       // content is dictated by the executable, not QProcess
process.error();                                       // returns error code e.g. QProcess::FailedToStart
process.kill();                                        // force kill the child process
process.exitStatus();                                  // NormalExit or CrashExit
process.exitCode();                                    // 0 on success
process.readAllStandardError();                        // grab error output

// Holds the thread until either the process finishes or if it hits the duration provided in Ms as a parameter
// Returns true if the process finished before the provided duration ends
// Returns false if the process didn't finished before the provided duration
// Also returns false if the process was already done before this line
process.waitForFinished(timeoutMs);
A Qt class that launches and manages external processes from within your applic­ation, used wherever you would use fork/exec in plain C++. It handles starting the process, managing its input/­output streams, waiting for it to finish, and inspecting the result. It is preferred over raw fork/exec in Qt codebases because it cooperates with Qt's own internal signal handling.

QProce­ss:­:nu­llD­evice()

QProcess::nullDevice()
A static method on QProcess that returns the path to the null device (/dev/null on Linux), which discards anything written to it and returns nothing when read. It is used to silence stdin and stdout when you only care about stderr. Being a static method it is called on the class itself rather than an instance.

QProce­ss:­:Fa­ile­dTo­Start

if (process.error() == QProcess::FailedToStart) {
    // executable not found or not runnable
}
An error code on QProcess that indicates the executable could not be launched at all, typically because it was not found on the system's PATH or the user lacks permission to run it. It is checked via proces­s.e­rror() after waitFo­rFi­nis­hed() returns false to distin­guish between a launch failure and a timeout. Being a static value it is referenced on the class itself rather than an instance.

QProce­ss:­:No­rma­lExit

if (process.exitStatus() == QProcess::NormalExit) {
    // process exited cleanly, exit code can be trusted
}
A status code on QProcess that indicates the process exited cleanly on its own, as opposed to being killed by a signal or crashing. It is checked via proces­s.e­xit­Sta­tus() after waitFo­rFi­nis­hed() returns to confirm the process shut down normally before trusting the exit code. Being a static value it is referenced on the class itself rather than an instance.

QProce­ss:­:No­tRu­nning

if (process.state() == QProcess::NotRunning) // process has exited
A value of the QProce­ss:­:Pr­oce­ssState enum indicating the process is not currently running — either it hasn't started yet or it has already finished. Check it via .state() to detect that a process exited between polling slices.