Classes - Annotated - Tree - Functions - Home - Structure

QProcess Class Reference

The QProcess class is used to start external programs and to communicate with them. More...

#include <qprocess.h>

Inherits QObject.

List of all member functions.

Public Members

Public Slots

Signals


Detailed Description

The QProcess class is used to start external programs and to communicate with them.

You can write to standard input of the started program. You can read the output of the program on standard output and standard error. You are notified when the program exits.

There are two different ways to run a process: If you use start(), you have full control over the process; you can write to the standard input via the writeToStdin() slots whenever you want, and you can close standard input via the closeStdin() slot.

If you know the data that should be written to the standard input of the process already when you want to run the process, you can use the launch() functions instead. These functions take the data that should be written to standard input as an argument, write it to standard input and automatically close standard input if all data was written.

If you use a launch() function to run the process, you should not use the slots writeToStdin() and closeStdin(), since the result is not well-defined.

Example: If you want to start the uic command (Qt commandline tool used with the Qt Designer) and make some operations on the output (the uic outputs the code it generates to standard output by default). Consider the case, that you want to start it with the command line options "-tr i18n" on the file "small_dialog.ui". On the command line you would do that with

  uic -tr i18n small_dialog.ui
  

A code snippet for this with the QProcess class could 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.
        // Keep in mind that the data might be reported in chunks.
    }

Please note that you need the quotes for the file on the command line, but you must not give the quotes to the QProcess: the command line shell processes the string and splits it into the arguments, but the QProcess class does not do this processing.

The readyReadStdout() signal is emitted when there is new data on standard output. This happens asynchronous: 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 are sure that all data is available when the slot is called. On the other hand, you must wait until the process has finished before doing any processing. Which approach is best, depends highly on the requirements of your application.

See also QSocket.


Member Function Documentation

QProcess::QProcess ( QObject * parent = 0, const char * name = 0 )

Constructs a QProcess object. The parameters parent and name are passed to the QObject constructor.

See also setArguments(), addArgument() and start().

QProcess::QProcess ( const QString & arg0, QObject * parent = 0, const char * name = 0 )

Constructs a QProcess with arg0 as the command to be executed. The parameters parent and name are passed to the QObject constructor.

The process is not started. You have to call start() explicitly to start the process.

See also setArguments(), addArgument() and start().

QProcess::QProcess ( const QStringList & args, QObject * parent = 0, const char * name = 0 )

Constructs a QProcess with args as the arguments of the process. The first element in the list is the command to be executed. The other elements in the list are the arguments to this command. The parameters parent and name are passed to the QObject constructor.

The process is not started. You have to call start() explicitly to start the process.

See also setArguments(), addArgument() and start().

QProcess::~QProcess ()

Destroys the class.

If the process is running, it is NOT terminated! Standard input, standard output and standard error of the process are closed.

See also hangUp() and kill().

void QProcess::addArgument ( const QString & arg ) [virtual]

Adds arg to the end of the existing list of arguments.

The first element in the list of arguments is the command to be executed; the following elements are the arguments to this command.

See also arguments() and setArguments().

Example: process/process.cpp.

QStringList QProcess::arguments () const

Returns the list of arguments that are set for the process. Arguments can be specified with the constructor or with the functions setArguments() and addArgument().

See also setArguments() and addArgument().

bool QProcess::canReadLineStderr () const

Returns TRUE if it's possible to read an entire line of text from standard error at this time, or FALSE if not.

See also readLineStderr() and canReadLineStdout().

bool QProcess::canReadLineStdout () const

Returns TRUE if it's possible to read an entire line of text from standard output at this time, or FALSE if not.

See also readLineStdout() and canReadLineStderr().

void QProcess::closeStdin () [virtual slot]

Closes standard input of the process.

This function also deletes pending data that is not written to standard input yet.

See also wroteToStdin().

int QProcess::exitStatus () const

Returns the exit status of the process. This function returns immediately and does not wait until the process is finished. In the case that the process is running, this function returns 0.

If normalExit() is FALSE, this function returns 0. So you should check the return value of normalExit() before relying on this value.

See also normalExit() and processExited().

void QProcess::hangUp () const

Asks the process to terminate. Processes can ignore this wish. If you want to be sure that the process really terminates, you must use kill() instead.

The function 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().

bool QProcess::isRunning () const

Returns TRUE if the process is running, otherwise FALSE.

See also normalExit(), exitStatus() and processExited().

void QProcess::kill () const

Terminates the process. This is not a safe way to end a process since the process will not be able to do cleanup. hangUp() is the saver way to do it, but processes might ignore a hangUp().

The function returns immediately: it does not wait until the process has finished. When the process really exited, the signal processExited() is emitted.

See also hangUp() and processExited().

bool QProcess::launch ( const QByteArray & buf, QStringList * env = 0 ) [virtual]

Runs the process and writes the data buf to standard input of the process. If all data is written to standard input, it closes standard input. The command is searched in the path for executable programs; you can also use an absolute path to the command.

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.

Returns TRUE if the process could be started, otherwise FALSE.

Notice 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 this data.

You can call this function when a process that was started with this instance still runs. In this case, it closes standard input of that process and it deletes pending data - 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.)

The object emits the signal launchFinished() when the task of this function call is finished; this might happen of two different reasons: either the starting of the process was not successful, then the object emits the signal immediately or if the start was successful, then the object emits the signal after it has written all data to standard input.

See also start() and launchFinished().

bool QProcess::launch ( const QString & buf, QStringList * env = 0 ) [virtual]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

The data buf is written to standard input with writeToStdin(): so this function writes the QString::local8Bit() representation of the string.

void QProcess::launchFinished () [signal]

This signal is emitted when the process was started with launch() and the launch() call has finished its tasks. This might happen of two different reasons: either the starting of the process was not successful, then the object emits the signal immediately or if the start was successful, then the object emits the signal after it has written all data to standard input.

See also launch() and QObject::deferredDelete().

bool QProcess::normalExit () const

Returns TRUE if the process has exited normally, otherwise FALSE. This implies that this function returns FALSE if the process is running.

See also isRunning(), exitStatus() and processExited().

void QProcess::processExited () [signal]

This signal is emitted when the process has exited.

See also isRunning(), normalExit(), exitStatus(), start() and launch().

Example: process/process.cpp.

PID QProcess::processIdentifier ()

Returns platform dependent information about the process. This can be used together with platform specific system calls.

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.

QString QProcess::readLineStderr () [virtual]

Reads a line of text from standard error, excluding any trailing newline or carriage return characters and returns it. Returns QString::null if canReadLineStderr() returns FALSE.

See also canReadLineStderr(), readyReadStderr(), readStderr() and readLineStdout().

QString QProcess::readLineStdout () [virtual]

Reads a line of text from standard output, excluding any trailing newline or carriage return characters and returns it. Returns QString::null if canReadLineStdout() returns FALSE.

See also canReadLineStdout(), readyReadStdout(), readStdout() and readLineStderr().

QByteArray QProcess::readStderr () [virtual]

Reads the data that the process has written to standard error. When new data was written to standard error, the class emits the signal readyReadStderr().

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().

QByteArray QProcess::readStdout () [virtual]

Reads the data that the process has written to standard output. When new data was written to standard output, the class emits the signal readyReadStdout().

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.

void QProcess::readyReadStderr () [signal]

This signal is emitted when the process wrote data to standard error. You can read the data with readStderr().

Please 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 loose any data.

See also readStderr(), readLineStderr() and readyReadStdout().

void QProcess::readyReadStdout () [signal]

This signal is emitted when the process wrote data to standard output. You can read the data with readStdout().

Please 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 loose any data.

See also readStdout(), readLineStdout() and readyReadStderr().

Example: process/process.cpp.

void QProcess::setArguments ( const QStringList & args ) [virtual]

Sets args as the arguments for the process. The first element in the list is the command to be executed. The other elements in the list are the arguments to this command.

Arguments that were previously set, are deleted first.

See also arguments() and addArgument().

void QProcess::setWorkingDirectory ( const QDir & dir ) [virtual]

Sets dir as the working directory for a process. This does not affect running processes; only processes that are started afterwards are affected.

Setting the working directory is especially useful for processes that try to access files with relative filenames.

See also workingDirectory() and start().

bool QProcess::start ( QStringList * env = 0 ) [virtual]

Tries to run a process for the command and arguments that were specified with setArguments(), addArgument() or that were specified in the constructor. The command is searched in the path for executable programs; you can also use an absolute path to the command.

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 enverionment varialbe 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 hangUp() 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.

QDir QProcess::workingDirectory () const

Returns the working directory that was set with setWorkingDirectory().

See also setWorkingDirectory().

void QProcess::writeToStdin ( const QByteArray & buf ) [virtual slot]

Writes the data buf to the standard input of the process. The process may or may not read this data.

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().

void QProcess::writeToStdin ( const QString & buf ) [virtual slot]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

The string buf is handled as a text: what is written to standard input is the QString::local8Bit() representation.

void QProcess::wroteToStdin () [signal]

This signal is emitted if the data send 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 loosing pending data.

See also writeToStdin() and closeStdin().


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta2