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.h>
Inherits QObject.
The QProcess class is used to start external programs and to communicate with them.
You can write to the started program's standard input, and can read the program's standard output and standard error. You can pass command line arguments to the program either in the constructor or with setArguments() or addArgument(). The program's working directory can be set with setWorkingDirectory(). If you need to set up environment variables pass them to the start() or launch() function (see below). The processExited() signal is emitted if the program exits. The program's exit status is available from exitStatus(), although you could simply call normalExit() to see if the program terminated normally.
There are two different ways to start a process. If you just want to run a program, optionally passing data to its standard input at the beginning, use one of the launch() functions. If you want full control of the program's standard input (especially if you don't know all data you want to send to standard input at the beginning), use the start() function.
If you use start() you can write to the program's standard input using writeToStdin() and you can close the standard input with closeStdin(). The wroteToStdin() signal is emitted if the data sent to standard input has been written. You can read from the program's standard output using readStdout() or readLineStdout(). These functions return an empty QByteArray if there is no data to read. The readyReadStdout() signal is emitted when there is data available to be read from standard output. Standard error has a set of functions that correspond to the standard output functions, i.e. readStderr(), readLineStderr() and readyReadStderr().
If you use one of the launch() functions the data you pass will be sent to the program's standard input which will be closed once all the data has been written. You should not use writeToStdin() or closeStdin() if you use launch(). If you need to send data to the program's standard input after it has started running use start() instead of launch().
Both start() and launch() can accept a string list of strings with the format, key=value, where the keys are the names of environment variables.
You can test to see if a program is running with isRunning(). The program's process identifier is available from processIdentifier(). If you want to terminate a running program use tryTerminate(), but note that the program may ignore this. If you really want to terminate the program, without it having any chance to clean up, you can use kill().
As an example, suppose we want to start the uic command (a Qt command line tool used with Qt Designer) and perform some operations on the output (the uic outputs the code it generates to standard output by default). Suppose further that we want to run the program on the file "small_dialog.ui" with the command line options "-tr i18n". On the command line we would write:
uic -tr i18n small_dialog.ui
A code snippet for this with the QProcess class might look like this:
UicManager::UicManager() {
proc = new QProcess( this );
proc->addArgument( "uic" ); proc->addArgument( "-tr" ); proc->addArgument( "i18n" ); proc->addArgument( "small_dialog.ui" ); connect( proc, SIGNAL(readyReadStdout()), this, SLOT(readFromStdout()) );
if ( !proc->start() ) { // error handling
} }
void UicManager::readFromStdout() { // Read and process the data. // Bear in mind that the data might be output in chunks.
}
Although you may need quotes for a file named on the command line (e.g. if it contains spaces) you shouldn't use extra quotes for arguments passed to addArgument() or setArguments().
The readyReadStdout() signal is emitted when there is new data on standard output. This happens asynchronously: you don't know if more data will arrive later. In the above example you could connect the processExited() signal to the slot UicManager::readFromStdout() instead. If you do so, you will be certain that all the data is available when the slot is called. On the other hand, you must wait until the process has finished before doing any processing.
See also QSocket, Input/Output and Networking and Miscellaneous Classes.
This enum type defines the communication channels connected to the process.
See also setCommunication() and communication().
See also setArguments(), addArgument() and start().
The process is not started. You must call start() or launch() to start the process.
See also setArguments(), addArgument() and start().
The process is not started. You must call start() or launch() to start the process.
See also setArguments(), addArgument() and start().
If the process is running, it is NOT terminated! Standard input, standard output and standard error of the process are closed.
You can connect the destroyed() signal to the kill() slot, if you want the process to be terminated automatically when the class is destroyed.
See also tryTerminate() and kill().
The first element in the list of arguments is the command to be executed; the following elements are the arguments to the command.
See also arguments() and setArguments().
Example: process/process.cpp.
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = myProcess.arguments(); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; }
See also setArguments() and addArgument().
See also readLineStderr() and canReadLineStdout().
See also readLineStdout() and canReadLineStderr().
See also setArguments() and addArgument().
This function also deletes pending data that is not written to standard input yet.
See also wroteToStdin().
See also setCommunication().
If normalExit() is FALSE (e.g. if the program was killed or crashed), this function returns 0, so you should check the return value of normalExit() before relying on this value.
See also normalExit() and processExited().
See also normalExit(), exitStatus() and processExited().
The nice way to end a process and to be sure that it is finished, is doing something like this:
process->tryTerminate(); QTimer::singleShot( 5000, process, SLOT( kill() ) );
This tries to terminate the process the nice way. If the process is still running after 5 seconds, it terminates the process the hard way. The timeout should be chosen depending on the time the process needs to do all the cleanup: use a higher value if the process is likely to do heavy computation on cleanup.
The slot returns immediately: it does not wait until the process has finished. When the process really exited, the signal processExited() is emitted.
See also tryTerminate() and processExited().
If env is null, then the process is started with the same environment as the starting process. If env is non-null, then the values in the stringlist are interpreted as environment setttings of the form key=value and the process is started with these environment settings. For convenience, there is a small exception to this rule under Unix: if env does not contain any settings for the environment variable LD_LIBRARY_PATH, then this variable is inherited from the starting process.
Returns TRUE if the process could be started; otherwise returns FALSE.
Note that you should not use the slots writeToStdin() and closeStdin() on processes started with launch(), since the result is not well-defined. If you need these slots, use start() instead.
The process may or may not read the buf data sent to its standard input.
You can call this function even when a process that was started with this instance is still running. Be aware that if you do this the standard input of the process that was launched first will be closed, with any pending data being deleted, and the process will be left to run out of your control. Similarly, if the process could not be started the standard input will be closed and the pending data deleted. (On operating systems that have zombie processes, Qt will also wait() on the old process.)
The object emits the signal launchFinished() when this function call is finished. If the start was successful, this signal is emitted after all the data has been written to standard input. If the start failed, then this signal is emitted immediately.
See also start() and launchFinished().
The data buf is written to standard input with writeToStdin() using the QString::local8Bit() representation of the strings.
This signal is emitted when the process was started with launch(). If the start was successful, this signal is emitted after all the data has been written to standard input. If the start failed, then this signal is emitted immediately.
This signal is especially useful if you want to know when you can safely delete the QProcess object for the case that you are not intrested in reading from standard output or standard error.
See also launch() and QObject::deleteLater().
See also isRunning(), exitStatus() and processExited().
This signal is emitted when the process has exited.
See also isRunning(), normalExit(), exitStatus(), start() and launch().
Example: process/process.cpp.
Under Unix the return value is the PID of the process, or -1 if no process is belonging to this object.
Under Windows it is a pointer to the PROCESS_INFORMATION struct, or 0 if no process is belonging to this object.
See also canReadLineStderr(), readyReadStderr(), readStderr() and readLineStdout().
See also canReadLineStdout(), readyReadStdout(), readStdout() and readLineStderr().
If there is no data to read, this function returns a QByteArray of size 0: it does not wait until there is something to read.
See also readyReadStderr(), readLineStderr(), readStdout() and writeToStdin().
If there is no data to read, this function returns a QByteArray of size 0: it does not wait until there is something to read.
See also readyReadStdout(), readLineStdout(), readStderr() and writeToStdin().
Example: process/process.cpp.
This signal is emitted when the process has written data to standard error. You can read the data with readStderr().
Note that this signal is only emitted when there is new data and not when there is old, but unread data. In the slot connected to this signal, you should always read everything that is available at that moment to make sure that you don't lose any data.
See also readStderr(), readLineStderr() and readyReadStdout().
This signal is emitted when the process has written data to standard output. You can read the data with readStdout().
Note that this signal is only emitted when there is new data and not when there is old, but unread data. In the slot connected to this signal, you should always read everything that is available at that moment to make sure that you don't lose any data.
See also readStdout(), readLineStdout() and readyReadStderr().
Example: process/process.cpp.
QProcess does not make any substitution of the arguments: if you specify "*" or "$DISPLAY", these values are passed to the process literally. If you want to have the same behavior as on the shell, you have to do the substitution yourself; i.e. instead of specifying a "*" you have to specify the list of all filenames and instead of the "$DISPLAY" you have to specify the value of the environment variable DISPLAY. The same applies for Windows, although a literal "*" as an argument is usually understood and substituted by Windows programs.
See also arguments() and addArgument().
commFlags is a bitwise OR between the flags defined in Communication.
The default is Stdin|Stdout|Stderr.
See also communication().
Setting the working directory is especially useful for processes that try to access files with relative filenames.
See also workingDirectory() and start().
If env is null, then the process is started with the same environment as the starting process. If env is non-null, then the values in the stringlist are interpreted as environment setttings of the form key=value and the process is started in these environment settings. For convenience, there is a small exception to this rule: under Unix, if env does not contain any settings for the environment variable LD_LIBRARY_PATH, then this variable is inherited from the starting process; under Windows the same applies for the environment variable PATH.
Returns TRUE if the process could be started, otherwise FALSE.
You can write data to standard input of the process with writeToStdin(), you can close standard input with closeStdin() and you can terminate the process tryTerminate() resp. kill().
You can call this function even when there already is a running process in this object. In this case, QProcess closes standard input of the old process and deletes pending data, i.e., you loose all control over that process, but the process is not terminated. This applies also if the process could not be started. (On operating systems that have zombie processes, Qt will also wait() on the old process.)
See also launch() and closeStdin().
Example: process/process.cpp.
The slot returns immediately: it does not wait until the process has finished. When the process really exited, the signal processExited() is emitted.
See also kill() and processExited().
See also setWorkingDirectory() and QDir::current().
This function returns immediately; the QProcess class might write the data at a later point (you have to enter the event loop for that). When all the data is written to the process, the signal wroteToStdin() is emitted. This does not mean that the process really read the data, since this class only detects when it was able to write the data to the operating system.
See also wroteToStdin(), closeStdin(), readStdout() and readStderr().
The string buf is handled as text using the QString::local8Bit() representation.
This signal is emitted if the data sent to standard input (via writeToStdin()) was actually written to the process. This does not imply that the process really read the data, since this class only detects when it was able to write the data to the operating system. But it is now safe to close standard input without losing pending data.
See also writeToStdin() and closeStdin().
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.4
|