Qtopia library API Documentation

QProcess Class Reference

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

#include <qprocess.h>

Inheritance diagram for QProcess:

Inheritance graph
[legend]
Collaboration diagram for QProcess:

Collaboration graph
[legend]
List of all members.

Public Types

typedef long Q_LONG
typedef Q_LONG PID
enum  Communication { Stdin = 0x01, Stdout = 0x02, Stderr = 0x04, DupStderr = 0x08 }

Public Slots

void tryTerminate () const
void kill () const
virtual void writeToStdin (const QByteArray &buf)
virtual void writeToStdin (const QString &buf)
virtual void closeStdin ()

Signals

void readyReadStdout ()
void readyReadStderr ()
void processExited ()
void wroteToStdin ()
void launchFinished ()

Public Member Functions

 QProcess (QObject *parent=0, const char *name=0)
 QProcess (const QString &arg0, QObject *parent=0, const char *name=0)
 QProcess (const QStringList &args, QObject *parent=0, const char *name=0)
 ~QProcess ()
QStringList arguments () const
void clearArguments ()
virtual void setArguments (const QStringList &args)
virtual void addArgument (const QString &arg)
QDir workingDirectory () const
virtual void setWorkingDirectory (const QDir &dir)
void setCommunication (int c)
int communication () const
virtual bool start (QStringList *env=0)
virtual bool launch (const QString &buf, QStringList *env=0)
virtual bool launch (const QByteArray &buf, QStringList *env=0)
bool isRunning () const
bool normalExit () const
int exitStatus () const
virtual QByteArray readStdout ()
virtual QByteArray readStderr ()
bool canReadLineStdout () const
bool canReadLineStderr () const
virtual QString readLineStdout ()
virtual QString readLineStderr ()
PID processIdentifier ()
void flushStdin ()

Protected Member Functions

void connectNotify (const char *signal)
void disconnectNotify (const char *signal)

Friends

class QProcessPrivate

Detailed Description

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

This is a temporary class. This will be replaced by Qt 3's QProcess class.

Definition at line 52 of file qprocess.h.


Member Enumeration Documentation

enum QProcess::Communication
 

This enum type defines the communication channels connected to the process.

Stdin Data can be written to the process's standard input.

Stdout Data can be read from the process's standard output.

Stderr Data can be read from the process's standard error.

DupStderr Duplicates standard error to standard output for new processes; i.e. everything that the process writes to standard error, is reported by QProcess on standard output instead. This is especially useful if your application requires that the output on standard output and standard error is read in the same order as the process output it. Please note that this is a binary flag, so if you want to activate this together with standard input, output and error redirection (the default), you have to specify {Stdin|Stdout|Stderr|DupStderr} for the setCommunication() call.

See also:
setCommunication() communication()

Definition at line 72 of file qprocess.h.


Constructor & Destructor Documentation

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

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

See also:
setArguments() addArgument() start()

Definition at line 90 of file qprocess.cpp.

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

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

The process is not started. You must call start() or launch() to start the process.

See also:
setArguments() addArgument() start()

Definition at line 108 of file qprocess.cpp.

References addArgument().

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 parent and name parameters are passed to the QObject constructor.

The process is not started. You must call start() or launch() to start the process.

See also:
setArguments() addArgument() start()

Definition at line 129 of file qprocess.cpp.

References setArguments().

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.

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

Definition at line 561 of file qprocess_unix.cpp.


Member Function Documentation

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

Definition at line 147 of file qprocess.cpp.

void QProcess::clearArguments  ) 
 

Clears the list of arguments that are set for the process.

See also:
setArguments() addArgument()

Definition at line 157 of file qprocess.cpp.

References QValueList::clear().

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 the command. Any previous arguments are deleted.

See also:
arguments() addArgument()

Definition at line 169 of file qprocess.cpp.

Referenced by QProcess().

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

Adds arg to the end of the list of arguments.

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

Definition at line 182 of file qprocess.cpp.

References QValueList::append().

Referenced by QProcess().

QDir QProcess::workingDirectory  )  const
 

Returns the working directory that was set with setWorkingDirectory(), or the current directory if none has been set.

See also:
setWorkingDirectory() QDir::current()

Definition at line 195 of file qprocess.cpp.

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

Definition at line 209 of file qprocess.cpp.

void QProcess::setCommunication int  commFlags  ) 
 

Sets commFlags as the communication required with the process.

commFlags is a bitwise OR between the flags defined in Communication.

The default is {Stdin|Stdout|Stderr}.

See also:
communication()

Definition at line 234 of file qprocess.cpp.

int QProcess::communication  )  const
 

Returns the communication required with the process.

See also:
setCommunication()

Definition at line 220 of file qprocess.cpp.

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

Definition at line 596 of file qprocess_unix.cpp.

References QDir::absPath(), QValueList::begin(), QObject::connect(), QString::contains(), QValueList::count(), QValueList::end(), QFile::exists(), QFileInfo::filePath(), QApplication::flushX(), QStringList::grep(), QFileInfo::isExecutable(), and QStringList::split().

Referenced by launch().

bool QProcess::launch const QString buf,
QStringList env = 0
[virtual]
 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The data buf is written to standard input with writeToStdin() using the QString::local8Bit() representation of the strings.

Definition at line 497 of file qprocess.cpp.

References closeStdin(), QObject::connect(), QString::isEmpty(), launchFinished(), start(), writeToStdin(), and wroteToStdin().

bool QProcess::launch const QByteArray buf,
QStringList env = 0
[virtual]
 

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

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() launchFinished();

Definition at line 474 of file qprocess.cpp.

References closeStdin(), QObject::connect(), QByteArray::isEmpty(), launchFinished(), start(), writeToStdin(), and wroteToStdin().

bool QProcess::isRunning  )  const
 

Returns TRUE if the process is running, otherwise FALSE.

See also:
normalExit() exitStatus() processExited()

Definition at line 874 of file qprocess_unix.cpp.

References exitNormal, and exitStat.

Referenced by exitStatus(), and normalExit().

bool QProcess::normalExit  )  const
 

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

See also:
isRunning() exitStatus() processExited()

Definition at line 246 of file qprocess.cpp.

References isRunning().

int QProcess::exitStatus  )  const
 

Returns the exit status of the process or 0 if the process is still running. This function returns immediately and does not wait until the process is finished.

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

Definition at line 266 of file qprocess.cpp.

References isRunning().

QByteArray QProcess::readStdout  )  [virtual]
 

Reads the data that the process has written to standard output. When new data is 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() writeToStdin()

Definition at line 286 of file qprocess.cpp.

References QByteArray::copy().

QByteArray QProcess::readStderr  )  [virtual]
 

Reads the data that the process has written to standard error. When new data is 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() writeToStdin()

Definition at line 310 of file qprocess.cpp.

References QByteArray::copy().

bool QProcess::canReadLineStdout  )  const
 

Returns TRUE if it's possible to read an entire line of text from standard output at this time; otherwise returns FALSE.

See also:
readLineStdout() canReadLineStderr()

Definition at line 330 of file qprocess.cpp.

References scanNewline().

bool QProcess::canReadLineStderr  )  const
 

Returns TRUE if it's possible to read an entire line of text from standard error at this time; otherwise returns FALSE.

See also:
readLineStderr() canReadLineStdout()

Definition at line 342 of file qprocess.cpp.

References scanNewline().

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

Definition at line 355 of file qprocess.cpp.

References QByteArray::isEmpty().

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

Definition at line 375 of file qprocess.cpp.

References QByteArray::isEmpty().

QProcess::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.

Definition at line 1150 of file qprocess_unix.cpp.

void QProcess::readyReadStdout  )  [signal]
 

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

Referenced by connectNotify(), and disconnectNotify().

void QProcess::readyReadStderr  )  [signal]
 

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

Referenced by connectNotify(), and disconnectNotify().

void QProcess::processExited  )  [signal]
 

This signal is emitted when the process has exited.

See also:
isRunning() normalExit() exitStatus() start() launch()

Referenced by connectNotify(), and disconnectNotify().

void QProcess::wroteToStdin  )  [signal]
 

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

Referenced by connectNotify(), disconnectNotify(), and launch().

void QProcess::launchFinished  )  [signal]
 

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.

See also:
launch() QObject::deleteLater()

Referenced by launch().

void QProcess::tryTerminate  )  const [slot]
 

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

Definition at line 833 of file qprocess_unix.cpp.

void QProcess::kill  )  const [slot]
 

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

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

Definition at line 863 of file qprocess_unix.cpp.

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

Definition at line 917 of file qprocess_unix.cpp.

Referenced by launch(), and writeToStdin().

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The string buf is handled as text using the QString::local8Bit() representation.

Definition at line 578 of file qprocess.cpp.

References QString::length(), QString::local8Bit(), QByteArray::resize(), and writeToStdin().

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

Definition at line 936 of file qprocess_unix.cpp.

Referenced by launch().

void QProcess::connectNotify const char *  signal  )  [protected]
 

Reimplemented from QObject.

Definition at line 596 of file qprocess.cpp.

References processExited(), readyReadStderr(), readyReadStdout(), and wroteToStdin().

void QProcess::disconnectNotify const char *  signal  )  [protected]
 

Reimplemented from QObject.

Definition at line 629 of file qprocess.cpp.

References processExited(), readyReadStderr(), readyReadStdout(), QObject::receivers(), and wroteToStdin().


The documentation for this class was generated from the following files:
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:25 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001