Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions | ![]() |
The QProcess class is used to start external programs and to communicate with them. More...
#include <QProcess>
Inherits QIODevice.
The QProcess class is used to start external programs and to communicate with them.
To start a process, pass the name and command line arguments of the program you want to run as arguments to start(). QProcess then enters the Starting state, and when the program has started, QProcess enters the Running state and emits started().
QProcess allows you to treat a process as a sequential I/O device. You can write to and read from the process just as you would access a network connection using QTcpSocket. You can then write to the process's standard input by calling write(), and read the standard output by calling read(), readLine() and getChar(). Because it inherits QIODevice, QProcess can also be used as an input source for QXmlReader or for generating data to be uploaded using QFtp.
Just before the process exits, QProcess enters the Finishing state and emits finishing(), allowing you to read any pending output from the process before the process dies. Finally, QProcess reenters the NotRunning state (the initial state) and emits finished().
The finished() signal provides the exit code of the process as an argument, and you can also call exitCode(), which returns the exit code of the last process that finished. At an error occurs at any point in time, QProcess will emit the error() signal. You can also call processError() to find the type of error that occurred last, and processState() to find the current process state.
Processes have two predefined output channels: the standard output channel (for regular console output) and the standard error channel (where errors are usually printed). These channels represent two separate streams of data. You can toggle between the two channels by calling setInputChannel(). QProcess emits readyRead() when data is available on the current input channel. It also emits readyReadStandardOutput() when new standard output data is available, and when new standard error data is available, readyReadStandardError() is emitted. Instead of calling read(), readLine(), or readChar(), you can explicitly read all data from either of the two channels by calling readAllStandardOutput() or readAllStandardError().
Certain processes need special environment settings in order to operate. You can set environment variables for your process by calling setEnvironment(). To set a working directory, call setWorkingDirectory(). By default, processes are run in the current working directory of the calling process.
QProcess provides a set of functions which allow it to be used without an event loop, by suspending the calling thread until certain signals are emitted:
Calling these functions from the main thread (the thread that calls QApplication::exec()) may cause your user interface to freeze.
The following example runs gzip to compress the string "Qt rocks!", without an event loop:
See also QBuffer, QFile, and QTcpSocket.
Constructs a QProcess object with the given parent.
Destructs the QProcess object.
Closes all communication with the process. After calling this function, QProcess will no longer emit readyRead(), and data can no longer be read or written.
Reimplemented from QIODevice.
Returns the environment that QProcess will use when starting a process, or an empty QStringList if no environment has been set. It no environment has been set, the environment of the calling process will be used.
See also setEnvironment().
Returns the exit code of the last process that finished.
Returns the current input channel of QProcess.
See also setInputChannel().
Returns the native process identifier for the running process, if available. If no process is currently running, 0 is returned.
Returns the type of error that occurred last.
See also error().
Returns the current state of the process.
See also stateChanged().
Regardless of the current input channel, this function returns all data available from the standard error of the process as a QByteArray.
See also readyReadStandardError(), readAllStandardOutput(), inputChannel(), and setInputChannel().
Regardless of the current input channel, this function returns all data available from the standard output of the process as a QByteArray.
See also readyReadStandardOutput(), readAllStandardError(), inputChannel(), and setInputChannel().
Sets the environment that QProcess will use when starting a process to environment.
See also environment().
Sets the current input channel of QProcess to channel. The current input channel is used by the functions read(), readAll(), readLine() and getChar(). It also decides which channel triggers QProcess to emit readyRead().
See also inputChannel().
Sets the working directory to dir. QProcess will start the process in this directory. The default behavior is to start the process in the working directory of the calling process.
See also setWorkingDirectory() and start().
Starts the program program in a new process, passing the command line arguments in arguments. QProcess will immediately enter the Starting state. If the process starts successfully, QProcess will emit started(); otherwise, error() will be emitted.
Terminates the current process, causing it to crash.
Blocks until the process has finished and the finished() signal has been emitted, or until msecs milliseconds have passed.
Returns true if the process finished; otherwise returns false (if the operation timed out or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
See also waitForFinished().
Blocks until the process has started and the started() signal has been emitted, or until msecs milliseconds have passed.
Returns true if the process was started successfully; otherwise returns false (if the operation timed out or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
See also waitForFinished().
Returns the working directory that the QProcess will enter before the program has started.
See also setWorkingDirectory().
Copyright © 2004 Trolltech | Trademarks | Qt 4.0.0-b1 |