
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQ3Process class is used to start external programs and to communicate with them.More...
| Header: | #include <Q3Process> |
| Inherits: | QObject |
| enum | Communication { Stdin, Stdout, Stderr, DupStderr } |
| Q3Process(QObject * parent = 0, const char * name = 0) | |
| Q3Process(const QString & arg0, QObject * parent = 0, const char * name = 0) | |
| Q3Process(const QStringList & args, QObject * parent = 0, const char * name = 0) | |
| ~Q3Process() | |
| virtual void | addArgument(const QString & arg) |
| QStringList | arguments() const |
| bool | canReadLineStderr() const |
| bool | canReadLineStdout() const |
| void | clearArguments() |
| int | communication() const |
| int | exitStatus() const |
| bool | isRunning() const |
| virtual bool | launch(const QByteArray & buf, QStringList * env = 0) |
| virtual bool | launch(const QString & buf, QStringList * env = 0) |
| bool | normalExit() const |
| PID | processIdentifier() |
| virtual QString | readLineStderr() |
| virtual QString | readLineStdout() |
| virtual QByteArray | readStderr() |
| virtual QByteArray | readStdout() |
| virtual void | setArguments(const QStringList & args) |
| void | setCommunication(int commFlags) |
| virtual void | setWorkingDirectory(const QDir & dir) |
| virtual bool | start(QStringList * env = 0) |
| QDir | workingDirectory() const |
| virtual void | closeStdin() |
| void | kill() const |
| void | tryTerminate() const |
| virtual void | writeToStdin(const QByteArray & buf) |
| virtual void | writeToStdin(const QString & buf) |
| void | launchFinished() |
| void | processExited() |
| void | readyReadStderr() |
| void | readyReadStdout() |
| void | wroteToStdin() |
| virtual void | connectNotify(const char * signal) |
| virtual void | disconnectNotify(const char * signal) |
TheQ3Process 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 withsetArguments() oraddArgument(). The program's working directory can be set withsetWorkingDirectory(). If you need to set up environment variables pass them to thestart() orlaunch() functions (see below). TheprocessExited() signal is emitted if the program exits. The program's exit status is available fromexitStatus(), although you could simply callnormalExit() 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 thelaunch() functions. If you want full control of the program's standard input (especially if you don't know all the data you want to send to standard input at the beginning), use thestart() function.
If you usestart() you can write to the program's standard input usingwriteToStdin() and you can close the standard input withcloseStdin(). ThewroteToStdin() signal is emitted if the data sent to standard input has been written. You can read from the program's standard output usingreadStdout() orreadLineStdout(). These functions return an emptyQByteArray if there is no data to read. ThereadyReadStdout() 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() andreadyReadStderr().
If you use one of thelaunch() 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 shouldnot usewriteToStdin() orcloseStdin() if you uselaunch(). If you need to send data to the program's standard input after it has started running usestart() instead oflaunch().
Bothstart() andlaunch() can accept a string list of strings each of which has the format, key=value, where the keys are the names of environment variables.
You can test to see if a program is running withisRunning(). The program's process identifier is available fromprocessIdentifier(). If you want to terminate a running program usetryTerminate(), but note that the program may ignore this. If youreally want to terminate the program, without it having any chance to clean up, you can usekill().
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 toaddArgument() orsetArguments().
ThereadyReadStdout() 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 theprocessExited() 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.
Note that if you are expecting a lot of output from the process, you may hit platform-dependent limits to the pipe buffer size. The solution is to make sure you connect to the output, e.g. thereadyReadStdout() andreadyReadStderr() signals and read the data as soon as it becomes available.
Please note thatQ3Process does not emulate a shell. This means thatQ3Process does not do any expansion of arguments: a '*' is passed as a '*' to the program and isnot replaced by all the files, a '$HOME' is also passed literally and isnot replaced by the environment variable HOME and the special characters for IO redirection ('>', '|', etc.) are also passed literally and donot have the special meaning as they have in a shell.
Also note thatQ3Process does not emulate a terminal. This means that certain programs which need direct terminal control, do not work as expected withQ3Process. Such programs include console email programs (like pine and mutt) but also programs which require the user to enter a password (like su and ssh).
Some Windows commands, for example,dir, are not provided by separate applications, but by the command interpreter. If you attempt to useQ3Process to execute these commands directly it won't work. One possible solution is to execute the command interpreter itself (cmd.exe on some Windows systems), and ask the interpreter to execute the desired command.
Under Windows there are certain problems starting 16-bit applications and capturing their output. Microsoft recommends using an intermediate application to start 16-bit applications.
See alsoQ3Socket.
This enum type defines the communication channels connected to the process.
| Constant | Value | Description |
|---|---|---|
Q3Process::Stdin | 0x01 | Data can be written to the process's standard input. |
Q3Process::Stdout | 0x02 | Data can be read from the process's standard output. |
Q3Process::Stderr | 0x04 | Data can be read from the process's standard error. |
Q3Process::DupStderr | 0x08 | Both the process's standard error outputand its standard output are written to its standard output. (Like Unix's dup2().) This means that nothing is sent to the standard error output. This is especially useful if your application requires that the output on standard output and on standard error must be read in the same order that they are produced. This is a flag, so to activate it you must passStdout|Stderr|DupStderr, orStdin|Stdout|Stderr|DupStderr if you want to provide input, to thesetCommunication() call. |
See alsosetCommunication() andcommunication().
Constructs aQ3Process object. Theparent andname parameters are passed to theQObject constructor.
See alsosetArguments(),addArgument(), andstart().
Constructs aQ3Process witharg0 as the command to be executed. Theparent andname parameters are passed to theQObject constructor.
The process is not started. You must callstart() orlaunch() to start the process.
See alsosetArguments(),addArgument(), andstart().
Constructs aQ3Process withargs 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. Theparent andname parameters are passed to theQObject constructor.
The process is not started. You must callstart() orlaunch() to start the process.
See alsosetArguments(),addArgument(), andstart().
Destroys the instance.
If the process is running, it is <b>not</b> terminated! The standard input, standard output and standard error of the process are closed.
You can connect thedestroyed() signal to thekill() slot, if you want the process to be terminated automatically when the instance is destroyed.
See alsotryTerminate() andkill().
[virtual]void Q3Process::addArgument(constQString & arg)Addsarg 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 command's arguments.
See alsoarguments() andsetArguments().
Returns the list of arguments that are set for the process. Arguments can be specified with the constructor or with the functionssetArguments() andaddArgument().
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 alsosetArguments() andaddArgument().
Returns true if it's possible to read an entire line of text from standard error at this time; otherwise returns false.
See alsoreadLineStderr() andcanReadLineStdout().
Returns true if it's possible to read an entire line of text from standard output at this time; otherwise returns false.
See alsoreadLineStdout() andcanReadLineStderr().
Clears the list of arguments that are set for the process.
See alsosetArguments() andaddArgument().
[virtual slot]void Q3Process::closeStdin()Closes the process's standard input.
This function also deletes any pending data that has not been written to standard input.
See alsowroteToStdin().
Returns the communication required with the process, i.e. some combination of theCommunication flags.
See alsosetCommunication().
[virtual protected]void Q3Process::connectNotify(constchar * signal)Reimplemented fromQObject::connectNotify().
[virtual protected]void Q3Process::disconnectNotify(constchar * signal)Reimplemented fromQObject::disconnectNotify().
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.
IfnormalExit() is false (e.g. if the program was killed or crashed), this function returns 0, so you should check the return value ofnormalExit() before relying on this value.
See alsonormalExit() andprocessExited().
Returns true if the process is running; otherwise returns false.
See alsonormalExit(),exitStatus(), andprocessExited().
[slot]void Q3Process::kill() constTerminates the process. This is not a safe way to end a process since the process will not be able to do any cleanup.tryTerminate() is safer, but processes can ignore atryTerminate().
The nice way to end a process and to be sure that it is finished, is to do 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 its cleanup: use a higher value if the process is likely to do a lot of computation or I/O on cleanup.
The slot returns immediately: it does not wait until the process has finished. When the process terminates, theprocessExited() signal is emitted.
See alsotryTerminate() andprocessExited().
[virtual]bool Q3Process::launch(constQByteArray & buf,QStringList * env = 0)Runs the process and writes the databuf 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.
Ifenv is null, then the process is started with the same environment as the starting process. Ifenv is non-null, then the values in the string list are interpreted as environment setttings of the formkey=value and the process is started with these environment settings. For convenience, there is a small exception to this rule under Unix: ifenv does not contain any settings for the environment variableLD_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 slotswriteToStdin() andcloseStdin() on processes started with launch(), since the result is not well-defined. If you need these slots, usestart() instead.
The process may or may not read thebuf 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 signallaunchFinished() 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 alsostart() andlaunchFinished().
[virtual]bool Q3Process::launch(constQString & buf,QStringList * env = 0)This is an overloaded function.
The databuf is written to standard input withwriteToStdin() using theQString::local8Bit() representation of the strings.
[signal]void Q3Process::launchFinished()This signal is emitted when the process was started withlaunch(). 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 theQ3Process object when you are not interested in reading from standard output or standard error.
See alsolaunch() andQObject::deleteLater().
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 alsoisRunning(),exitStatus(), andprocessExited().
[signal]void Q3Process::processExited()This signal is emitted when the process has exited.
See alsoisRunning(),normalExit(),exitStatus(),start(), andlaunch().
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 belongs to this object.
Under Windows it is a pointer to thePROCESS_INFORMATION struct, or 0 if no process is belongs to this object.
Use of this function's return value is likely to be non-portable.
[virtual]QString Q3Process::readLineStderr()Reads a line of text from standard error, excluding any trailing newline or carriage return characters and returns it. Returns an empty string ifcanReadLineStderr() returns false.
By default, the text is interpreted to be in Latin-1 encoding. If you need other codecs, you can set a different codec withQTextCodec::setCodecForCStrings().
See alsocanReadLineStderr(),readyReadStderr(),readStderr(), andreadLineStdout().
[virtual]QString Q3Process::readLineStdout()Reads a line of text from standard output, excluding any trailing newline or carriage return characters, and returns it. Returns an empty string ifcanReadLineStdout() returns false.
By default, the text is interpreted to be in Latin-1 encoding. If you need other codecs, you can set a different codec withQTextCodec::setCodecForCStrings().
See alsocanReadLineStdout(),readyReadStdout(),readStdout(), andreadLineStderr().
[virtual]QByteArray Q3Process::readStderr()Reads the data that the process has written to standard error. When new data is written to standard error, the class emits the signalreadyReadStderr().
If there is no data to read, this function returns aQByteArray of size 0: it does not wait until there is something to read.
See alsoreadyReadStderr(),readLineStderr(),readStdout(), andwriteToStdin().
[virtual]QByteArray Q3Process::readStdout()Reads the data that the process has written to standard output. When new data is written to standard output, the class emits the signalreadyReadStdout().
If there is no data to read, this function returns aQByteArray of size 0: it does not wait until there is something to read.
See alsoreadyReadStdout(),readLineStdout(),readStderr(), andwriteToStdin().
[signal]void Q3Process::readyReadStderr()This signal is emitted when the process has written data to standard error. You can read the data withreadStderr().
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 alsoreadStderr(),readLineStderr(), andreadyReadStdout().
[signal]void Q3Process::readyReadStdout()This signal is emitted when the process has written data to standard output. You can read the data withreadStdout().
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 alsoreadStdout(),readLineStdout(), andreadyReadStderr().
[virtual]void Q3Process::setArguments(constQStringList & args)Setsargs 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.
Q3Process does not perform argument substitutions; for example, if you specify "*" or "$DISPLAY", these values are passed to the process literally. If you want to have the same behavior as the shell provides, you must do the substitutions yourself; i.e. instead of specifying a "*" you must specify the list of all the filenames in the current directory, and instead of "$DISPLAY" you must specify the value of the environment variableDISPLAY.
Note for Windows users. The standard Windows shells, e.g.command.com andcmd.exe, do not perform file globbing, i.e. they do not convert a "*" on the command line into a list of files in the current directory. For this reason most Windows applications implement their own file globbing, and as a result of this, specifying an argument of "*" for a Windows application is likely to result in the application performing a file glob and ending up with a list of filenames.
See alsoarguments() andaddArgument().
SetscommFlags as the communication required with the process.
commFlags is a bitwise OR of the flags defined by theCommunication enum.
The default isStdin|Stdout|Stderr.
See alsocommunication().
[virtual]void Q3Process::setWorkingDirectory(constQDir & dir)Setsdir as the working directory for processes. 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 paths.
See alsoworkingDirectory() andstart().
[virtual]bool Q3Process::start(QStringList * env = 0)Tries to run a process for the command and arguments that were specified withsetArguments(),addArgument() or that were specified in the constructor. The command is searched for in the path for executable programs; you can also use an absolute path in the command itself.
Ifenv is null, then the process is started with the same environment as the starting process. Ifenv is non-null, then the values in the stringlist are interpreted as environment setttings of the formkey=value and the process is started in these environment settings. For convenience, there is a small exception to this rule: under Unix, ifenv does not contain any settings for the environment variableLD_LIBRARY_PATH, then this variable is inherited from the starting process; under Windows the same applies for the environment variablePATH.
Returns true if the process could be started; otherwise returns false.
You can write data to the process's standard input withwriteToStdin(). You can close standard input withcloseStdin() and you can terminate the process withtryTerminate(), or withkill().
You can call this function even if you've used this instance to create a another process which is still running. In such cases,Q3Process closes the old process's standard input and deletes pending data, i.e., you lose all control over the old process, but the old 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 alsolaunch() andcloseStdin().
[slot]void Q3Process::tryTerminate() constAsks the process to terminate. Processes can ignore this if they wish. If you want to be certain that the process really terminates, you can usekill() instead.
The slot returns immediately: it does not wait until the process has finished. When the process terminates, theprocessExited() signal is emitted.
See alsokill() andprocessExited().
Returns the working directory that was set withsetWorkingDirectory(), or the current directory if none has been explicitly set.
See alsosetWorkingDirectory() andQDir::current().
[virtual slot]void Q3Process::writeToStdin(constQByteArray & buf)Writes the databuf to the process's standard input. The process may or may not read this data.
This function returns immediately; theQ3Process class might write the data at a later point (you must enter the event loop for this to occur). When all the data is written to the process, the signalwroteToStdin() is emitted. This does not mean that the process actually read the data, since this class only detects when it was able to write the data to the operating system.
See alsowroteToStdin(),closeStdin(),readStdout(), andreadStderr().
[virtual slot]void Q3Process::writeToStdin(constQString & buf)This is an overloaded function.
The stringbuf is handled as text using theQString::local8Bit() representation.
[signal]void Q3Process::wroteToStdin()This signal is emitted if the data sent to standard input (viawriteToStdin()) 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 alsowriteToStdin() andcloseStdin().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.