QProcess 类

The QProcess 类用于启动外部程序并与之通信。 更多...

头: #include <QProcess>
继承: QIODevice

注意: 此类的所有函数 可重入 .

公共类型

enum ExitStatus { NormalExit, CrashExit }
enum ProcessChannel { StandardOutput, StandardError }
enum ProcessChannelMode { SeparateChannels, MergedChannels, ForwardedChannels }
enum ProcessError { FailedToStart, Crashed, Timedout, WriteError, ReadError, UnknownError }
enum ProcessState { NotRunning, Starting, Running }

公共函数

QProcess (QObject * parent = 0)
virtual ~QProcess ()
void closeReadChannel (ProcessChannel channel )
void closeWriteChannel ()
QProcess::ProcessError error () const
int exitCode () const
QProcess::ExitStatus exitStatus () const
QString nativeArguments () const
Q_PID pid () const
ProcessChannelMode processChannelMode () const
QProcessEnvironment processEnvironment () const
QByteArray readAllStandardError ()
QByteArray readAllStandardOutput ()
ProcessChannel readChannel () const
void setNativeArguments (const QString & arguments )
void setProcessChannelMode (ProcessChannelMode mode )
void setProcessEnvironment (const QProcessEnvironment & environment )
void setReadChannel (ProcessChannel channel )
void setStandardErrorFile (const QString & fileName , OpenMode mode = Truncate)
void setStandardInputFile (const QString & fileName )
void setStandardOutputFile (const QString & fileName , OpenMode mode = Truncate)
void setStandardOutputProcess (QProcess * destination )
void setWorkingDirectory (const QString & dir )
void start (const QString & program , const QStringList & arguments , OpenMode mode = ReadWrite)
void start (const QString & program , OpenMode mode = ReadWrite)
QProcess::ProcessState state () const
bool waitForFinished (int msecs = 30000)
bool waitForStarted (int msecs = 30000)
QString workingDirectory () const

重实现公共函数

virtual bool atEnd () const
virtual qint64 bytesAvailable () const
virtual qint64 bytesToWrite () const
virtual bool canReadLine () const
virtual void close ()
virtual bool isSequential () const
virtual bool waitForBytesWritten (int msecs = 30000)
virtual bool waitForReadyRead (int msecs = 30000)

公共槽

void kill ()
void terminate ()

信号

void error (QProcess::ProcessError error )
void finished (int exitCode , QProcess::ExitStatus exitStatus )
void readyReadStandardError ()
void readyReadStandardOutput ()
void started ()
void stateChanged (QProcess::ProcessState newState )

静态公共成员

int execute (const QString & program , const QStringList & arguments )
int execute (const QString & program )
bool startDetached (const QString & program , const QStringList & arguments , const QString & workingDirectory , qint64 * pid = 0)
bool startDetached (const QString & program , const QStringList & arguments )
bool startDetached (const QString & program )
QStringList systemEnvironment ()

保护函数

void setProcessState (ProcessState state )
virtual void setupChildProcess ()

重实现保护函数

virtual qint64 readData (char * data , qint64 maxlen )
virtual qint64 writeData (const char * data , qint64 len )
typedef Q_PID

额外继承成员

详细描述

The QProcess 类用于启动外部程序并与之通信。

运行进程

要启动进程,把希望运行的程序名称和命令行自变量作为自变量传递给 start ()。自变量被供给作为个体字符串在 QStringList .

For example, the following code snippet runs the analog clock example in the Motif style on X11 platforms by passing strings containing "-style" and "motif" as two items in the list of arguments:

    QObject *parent;
    ...
    QString program = "./path/to/Qt/examples/widgets/analogclock";
    QStringList arguments;
    arguments << "-style" << "motif";
    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);
					

QProcess then enters the Starting state, and when the program has started, QProcess 进入 运行 状态并发射 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 。然后可以写入进程标准输入通过调用 write (),和读取标准输出通过调用 read (), readLine (),和 getChar ()。因为它继承 QIODevice , QProcess can also be used as an input source for QXmlReader ,或对于生成要上传的数据使用 QFtp .

注意: On Windows CE and Symbian, reading and writing to a process is not supported.

When the process exits, QProcess reenters the NotRunning 状态 (初始状态),并发射 finished ().

The finished () 信号以自变量形式提供进程退出代码和退出状态,也可以调用 exitCode () 以获得最后完成进程的退出代码,和 exitStatus () to obtain its exit status. If an error occurs at any point in time, QProcess 将发射 error () 信号。也可以调用 error () 以查找最后出现的错误类型,和 state () 以查找当前进程状态。

凭借通道通信

进程有 2 个预定义输出通道:标准输出通道 ( stdout ) 供给常规控制台输出,和标准错误通道 ( stderr ) 通常提供由进程打印的错误。这些通道表示 2 个单独数据流。可以在它们之间切换通过调用 setReadChannel (). QProcess 发射 readyRead () 当当前读取通道数据可用时。它还发射 readyReadStandardOutput () 当新标准输出数据可用时,和当新标准错误数据可用时, readyReadStandardError () 被发射。代替调用 read (), readLine (),或 getChar (),可以明确读取 2 通道之一的所有数据通过调用 readAllStandardOutput () 或 readAllStandardError ().

The terminology for the channels can be misleading. Be aware that the process's output channels correspond to QProcess 's read channels, whereas the process's input channels correspond to QProcess 's write channels. This is because what we read using QProcess is the process's output, and what we write becomes the process's input.

QProcess can merge the two output channels, so that standard output and standard error data from the running process both use the standard output channel. Call setProcessChannelMode () 采用 MergedChannels before starting the process to activative this feature. You also have the option of forwarding the output of the running process to the calling, main process, by passing ForwardedChannels as the argument.

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 ()。默认情况下,进程在调用进程的当前工作目录下运行。

注意: On Symbian, setting environment or working directory is not supported. The working directory will always be the private directory of the running process.

注意: On QNX, setting the working directory may cause all application threads, with the exception of the QProcess caller thread, to temporarily freeze, owing to a limitation in the operating system.

同步进程 API

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:

从主线程调用这些函数 (线程调用 QApplication::exec ()) 可能导致用户界面被冻结。

以下范例运行 gzip 以压缩字符串 Qt rocks!,没有事件循环:

    QProcess gzip;
    gzip.start("gzip", QStringList() << "-c");
    if (!gzip.waitForStarted())
        return false;
    gzip.write("Qt rocks!");
    gzip.closeWriteChannel();
    if (!gzip.waitForFinished())
        return false;
    QByteArray result = gzip.readAll();
					

Windows 用户注意事项

某些 Windows 命令 (例如, dir ) are not provided by separate applications, but by the command interpreter itself. If you attempt to use QProcess to execute these commands directly, it won't work. One possible solution is to execute the command interpreter itself ( cmd.exe 在某些 Windows 系统),和要求解释器执行期望命令。

Symbian Platform Security Requirements

On Symbian, processes which use the functions kill () 或 terminate () must have the PowerMgmt platform security capability. If the client process lacks this capability, these functions will fail.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.

另请参阅 QBuffer , QFile ,和 QTcpSocket .

成员类型文档编制

enum QProcess:: ExitStatus

此枚举描述不同退出状态为 QProcess .

常量 描述
QProcess::NormalExit 0 进程正常退出。
QProcess::CrashExit 1 进程崩溃。

另请参阅 exitStatus ().

enum QProcess:: ProcessChannel

此枚举描述正运行进程所使用的进程通道。传递这些值之一给 setReadChannel () 设置当前读取通道为 QProcess .

常量 描述
QProcess::StandardOutput 0 正运行进程的 stdout (标准输出)。
QProcess::StandardError 1 正运行进程的 stderr (标准错误)。

另请参阅 setReadChannel ().

enum QProcess:: ProcessChannelMode

This enum describes the process channel modes of QProcess 。将这些值之一传递给 setProcessChannelMode () 以设置当前读取通道模式。

常量 描述
QProcess::SeparateChannels 0 QProcess 管理正运行进程的输出,将标准输出和标准错误数据保持在单独内部缓冲中。可以选择 QProcess 的当前读取通道通过调用 setReadChannel ()。这是默认通道模式对于 QProcess .
QProcess::MergedChannels 1 QProcess 将在运行进程的输出合并到标准输出通道 ( stdout )。标准错误通道 ( stderr ) 将不接收任何数据。在运行进程的标准输出和标准错误数据是交错的。
QProcess::ForwardedChannels 2 QProcess 将正运行进程的输出转发到主进程。由子级进程写入其标准输出和标准错误的任何内容,都将写入主进程的标准输出和标准错误。

另请参阅 setProcessChannelMode ().

enum QProcess:: ProcessError

此枚举描述错误的不同类型,报告通过 QProcess .

常量 描述
QProcess::FailedToStart 0 The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program.
QProcess::Crashed 1 进程有时崩溃在成功启动后。
QProcess::Timedout 2 最后 waitFor...() 函数超时。状态对于 QProcess 不变,和可以再次试着调用 waitFor...()。
QProcess::WriteError 4 发生错误当试图写入进程时。例如,进程可能未运行,或它可能已关闭其输入通道。
QProcess::ReadError 3 发生错误当试图从进程读取时。例如,进程可能未运行。
QProcess::UnknownError 5 发生未知错误。这是默认返回值为 error ().

另请参阅 error ().

enum QProcess:: ProcessState

此枚举描述不同状态为 QProcess .

常量 描述
QProcess::NotRunning 0 进程未运行。
QProcess::Starting 1 进程正在启动,但尚未援引程序。
QProcess::Running 2 进程正在运行且读写就绪。

另请参阅 state ().

成员函数文档编制

QProcess:: QProcess ( QObject * parent = 0)

构造 QProcess 对象采用给定 parent .

[虚拟] QProcess:: ~QProcess ()

销毁 QProcess 对象,即:杀除进程。

注意:此函数不会返回直到进程被终止。

[虚拟] bool QProcess:: atEnd () const

重实现自 QIODevice::atEnd ().

Returns true if the process is not running, and no more data is available for reading; otherwise returns false.

[虚拟] qint64 QProcess:: bytesAvailable () const

重实现自 QIODevice::bytesAvailable ().

[虚拟] qint64 QProcess:: bytesToWrite () const

重实现自 QIODevice::bytesToWrite ().

[虚拟] bool QProcess:: canReadLine () const

重实现自 QIODevice::canReadLine ().

此函数运转于当前读取通道。

另请参阅 readChannel () 和 setReadChannel ().

[虚拟] void QProcess:: close ()

重实现自 QIODevice::close ().

关闭进程的所有通信并杀除它。在调用此函数后, QProcess 将不再发射 readyRead (),且无法再读取或写入数据。

void QProcess:: closeReadChannel ( ProcessChannel channel )

关闭读取通道 channel 。在调用此函数后, QProcess 将不再接收通道数据。任何已收到的数据仍可用于读取。

调用此函数以节省内存,若对进程的输出不感兴趣。

另请参阅 closeWriteChannel () 和 setReadChannel ().

void QProcess:: closeWriteChannel ()

调度写入通道对于 QProcess 要被关闭。一旦所有数据被写入进程,通道就将关闭。在调用此函数后,任何写入进程的试图都将失败。

关闭写入通道是必要的,对于读取输入数据 (直到通道被关闭为止) 的程序而言。例如,程序 more 用于在 Unix 和 Windows 控制台中显示文本数据。但它不会显示文本数据直到 QProcess 的写入通道被关闭。范例:

QProcess more;
more.start("more");
more.write("Text to display");
more.closeWriteChannel();
// QProcess will emit readyRead() once "more" starts printing
					

写入通道被隐式打开当 start () 被调用。

另请参阅 closeReadChannel ().

QProcess::ProcessError QProcess:: error () const

返回最后出现的错误类型。

另请参阅 state ().

[signal] void QProcess:: error ( QProcess::ProcessError error )

此信号被发射,当进程出现错误时。指定 error 描述出现的错误类型。

注意: 信号 error 在此类中是重载。要使用函数指针句法连接到此信号,必须在静态铸造中指定信号类型,如此范例所示:

connect(process, static_cast<void(QProcess::*)(QProcess::ProcessError)>(&QProcess::error),
    [=](QProcess::ProcessError error){ /* ... */ });
					

[static] int QProcess:: execute (const QString & program , const QStringList & arguments )

启动程序 program 采用自变量 arguments 在新进程中,等待它完成,然后返回进程的退出代码。新进程写入控制台的任何数据会被转发给调用进程。

环境和工作目录继承自调用进程。

On Windows, arguments that contain spaces are wrapped in quotes.

若进程无法启动,返回 -2。若进程崩溃,返回 -1。否则,返回进程的退出代码。

[static] int QProcess:: execute (const QString & program )

这是重载函数。

启动程序 program 在新的进程中。 program is a single string of text containing both the program name and its arguments. The arguments are separated by one or more spaces.

int QProcess:: exitCode () const

返回最后完成进程的退出代码。

QProcess::ExitStatus QProcess:: exitStatus () const

返回最后完成的进程退出状态。

On Windows, if the process was terminated with TerminateProcess() from another application this function will still return NormalExit 除非退出代码小于 0。

该函数在 Qt 4.1 引入。

[signal] void QProcess:: finished ( int exitCode , QProcess::ExitStatus exitStatus )

此信号被发射当进程完成时。 exitCode is the exit code of the process, and exitStatus 是退出状态。在进程完成后,缓冲在 QProcess 仍完好无损。仍可以读取进程可能已写入的任何数据在完成之前。

注意: 信号 finished 在此类中是重载。要使用函数指针句法连接到此信号,必须在静态铸造中指定信号类型,如此范例所示:

connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
    [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ });
					

另请参阅 exitStatus ().

[虚拟] bool QProcess:: isSequential () const

重实现自 QIODevice::isSequential ().

[slot] void QProcess:: kill ()

杀除当前进程,导致它立即退出。

On Windows, kill() uses TerminateProcess, and on Unix and Mac OS X, the SIGKILL signal is sent to the process.

On Symbian, this function requires platform security capability PowerMgmt . If absent, the process will panic with KERN-EXEC 46.

注意: Killing running processes from other processes will typically cause a panic in Symbian due to platform security.

另请参阅 Symbian Platform Security Requirements and terminate ().

QString QProcess:: nativeArguments () const

返回程序的附加本机命令行自变量。

注意: This function is available only on the Windows and Symbian platforms.

该函数在 Qt 4.7 引入。

另请参阅 setNativeArguments ().

Q_PID QProcess:: pid () const

Returns the native process identifier for the running process, if available. If no process is currently running, 0 is returned.

ProcessChannelMode QProcess:: processChannelMode () const

返回通道模式对于 QProcess 标准输出和标准错误通道。

该函数在 Qt 4.2 引入。

另请参阅 setProcessChannelMode (), ProcessChannelMode ,和 setReadChannel ().

QProcessEnvironment QProcess:: processEnvironment () const

返回环境从 QProcess will use when starting a process, or an empty object if no environment has been set using setEnvironment() or setProcessEnvironment ()。若没有设置环境,将使用调用进程的环境。

注意: The environment settings are ignored on Windows CE, as there is no concept of an environment.

该函数在 Qt 4.6 引入。

另请参阅 setProcessEnvironment (), setEnvironment (),和 QProcessEnvironment::isEmpty ().

QByteArray QProcess:: readAllStandardError ()

不管当前读取通道,此函数从进程标准错误返回所有可用数据按 QByteArray .

另请参阅 readyReadStandardError (), readAllStandardOutput (), readChannel (),和 setReadChannel ().

QByteArray QProcess:: readAllStandardOutput ()

不管当前读取通道,此函数从进程标准输出返回所有可用数据按 QByteArray .

另请参阅 readyReadStandardOutput (), readAllStandardError (), readChannel (),和 setReadChannel ().

ProcessChannel QProcess:: readChannel () const

返回当前读取通道为 QProcess .

另请参阅 setReadChannel ().

[virtual protected] qint64 QProcess:: readData ( char * data , qint64 maxlen )

重实现自 QIODevice::readData ().

[signal] void QProcess:: readyReadStandardError ()

此信号被发射当进程已使新数据可用透过其标准错误通道 ( stderr )。它被发射不管当前 读取通道 .

另请参阅 readAllStandardError () 和 readChannel ().

[signal] void QProcess:: readyReadStandardOutput ()

此信号被发射当进程已使新数据可用透过其标准输出通道 ( stdout )。它被发射不管当前 读取通道 .

另请参阅 readAllStandardOutput () 和 readChannel ().

void QProcess:: setNativeArguments (const QString & arguments )

这是重载函数。

设置额外本机命令行 arguments 为程序。

在操作系统中,若系统 API 用于传递命令行 arguments 到本机子进程使用单字符串,可以设想无法传递命令行凭借 QProcess 基于列表的可移植 API。在这种情况下,必须使用此函数来设置字符串, appended 到通常由自变量列表合成的字符串,带定界空格。

注意: This function is available only on the Windows and Symbian platforms.

该函数在 Qt 4.7 引入。

另请参阅 nativeArguments ().

void QProcess:: setProcessChannelMode ( ProcessChannelMode mode )

设置通道模式为 QProcess 标准输出和标准错误通道到 mode 指定。会使用此模式当下次 start () 被调用。例如:

QProcess builder;
builder.setProcessChannelMode(QProcess::MergedChannels);
builder.start("make", QStringList() << "-j2");
if (!builder.waitForFinished())
    qDebug() << "Make failed:" << builder.errorString();
else
    qDebug() << "Make output:" << builder.readAll();
					

该函数在 Qt 4.2 引入。

另请参阅 processChannelMode (), ProcessChannelMode ,和 setReadChannel ().

void QProcess:: setProcessEnvironment (const QProcessEnvironment & environment )

Sets the environment that QProcess will use when starting a process to the environment 对象。

For example, the following code adds the C:\\BIN directory to the list of executable paths ( PATHS ) on Windows and sets TMPDIR :

QProcess process;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable
env.insert("PATH", env.value("Path") + ";C:\\Bin");
process.setProcessEnvironment(env);
process.start("myapp");
					

注意,在 Windows 环境变量名不区分大小写。

该函数在 Qt 4.6 引入。

另请参阅 processEnvironment (), QProcessEnvironment::systemEnvironment (),和 setEnvironment ().

[protected] void QProcess:: setProcessState ( ProcessState state )

设置当前状态为 QProcess state 指定。

另请参阅 state ().

void QProcess:: setReadChannel ( ProcessChannel channel )

设置当前读取通道为 QProcess 到给定 channel 。当前输入通道用于函数 read (), readAll (), readLine (),和 getChar ()。它还确定哪个通道触发 QProcess 以发射 readyRead ().

另请参阅 readChannel ().

void QProcess:: setStandardErrorFile (const QString & fileName , OpenMode mode = Truncate)

将进程的标准错误重定向到文件 fileName 。当重定向到位时,标准错误读取通道被关闭:读取它使用 read () 将始终失败,就像 readAllStandardError ()。文件将被追加若 mode 为 Append,否则, 它将被截取。

setStandardOutputFile () 了解如何打开文件的更多相关信息。

注意:若 setProcessChannelMode () 被调用采用自变量 QProcess::MergedChannels ,此函数不起作用。

该函数在 Qt 4.2 引入。

另请参阅 setStandardInputFile (), setStandardOutputFile (),和 setStandardOutputProcess ().

void QProcess:: setStandardInputFile (const QString & fileName )

将进程的标准输入重定向到文件指示通过 fileName 。当输入重定向到位时, QProcess 对象将处于只读模式 (调用 write () 将导致出错)。

若文件 fileName 不存在此刻 start () 被调用或不可读,启动进程将失败。

在进程启动后调用 setStandardInputFile() 不起作用。

该函数在 Qt 4.2 引入。

另请参阅 setStandardOutputFile (), setStandardErrorFile (),和 setStandardOutputProcess ().

void QProcess:: setStandardOutputFile (const QString & fileName , OpenMode mode = Truncate)

将进程的标准输出重定向到文件 fileName 。当重定向到位时,标准输出读取通道被关闭:读取它使用 read () 将始终失败,就像 readAllStandardOutput ().

若文件 fileName 不存在此刻 start () 被调用,它将被创建。若它无法被创建,启动将失败。

若文件存在且 mode is QIODevice::Truncate , the file will be truncated. Otherwise (if mode is QIODevice::Append ), the file will be appended to.

在进程启动后调用 setStandardOutputFile() 不起作用。

该函数在 Qt 4.2 引入。

另请参阅 setStandardInputFile (), setStandardErrorFile (),和 setStandardOutputProcess ().

void QProcess:: setStandardOutputProcess ( QProcess * destination )

将此进程的标准输出流管道到 destination 进程的标准输入。

以下 Shell 命令:

command1 | command2
					

Can be accomplished with QProcesses with the following code:

QProcess process1;
QProcess process2;
process1.setStandardOutputProcess(&process2);
process1.start("command1");
process2.start("command2");
					

该函数在 Qt 4.2 引入。

void QProcess:: setWorkingDirectory (const QString & dir )

把工作目录设为 dir . QProcess 将在此目录下启动进程。默认行为是在调用进程的工作目录下启动进程。

注意: The working directory setting is ignored on Symbian; the private directory of the process is considered its working directory.

注意: 在 QNX,这可能导致所有应用程序线程被临时冻结。

另请参阅 workingDirectory () 和 start ().

[virtual protected] void QProcess:: setupChildProcess ()

This function is called in the child process context just before the program is executed on Unix or Mac OS X (i.e., after fork() ,但先于 execve() )。重实现此函数,以履行最后时刻初始化子级进程。范例:

class SandboxProcess : public QProcess
{
    ...
 protected:
     void setupChildProcess();
    ...
};
void SandboxProcess::setupChildProcess()
{
    // Drop all privileges in the child process, and enter
    // a chroot jail.
#if defined Q_OS_UNIX
    ::setgroups(0, 0);
    ::chroot("/etc/safe");
    ::chdir("/");
    ::setgid(safeGid);
    ::setuid(safeUid);
    ::umask(0);
#endif
}
					

无法退出进程 (例如:通过调用 exit()) 从此函数。若需要在开始执行前停止程序,解决方案是发射 finished () 然后调用 exit()。

警告: 此函数被调用通过 QProcess on Unix and Mac OS X only. On Windows and QNX, it is not called.

void QProcess:: start (const QString & program , const QStringList & arguments , OpenMode mode = ReadWrite)

启动给定 program in a new process, if none is already running, passing the command line arguments in arguments OpenMode 被设为 mode .

The QProcess 对象将立即进入 Starting 状态。若进程成功启动, QProcess 将发射 started ();否则, error () will be emitted. If the QProcess object is already running a process, a warning may be printed at the console, and the existing process will continue running.

注意: 进程是异步启动的,这意味着 started () 和 error () 信号可能被延迟。调用 waitForStarted () 以确保进程已启动 (或启动失败) 且这些信号已被发射。

注意: 不履行进一步的自变量分割。

Windows: Arguments that contain spaces are wrapped in quotes.

另请参阅 pid (), started (),和 waitForStarted ().

void QProcess:: start (const QString & program , OpenMode mode = ReadWrite)

这是重载函数。

启动程序 program in a new process, if one is not already running. program 是包含程序名称及其自变量的单文本字符串。自变量由一个或多个空格分隔。例如:

QProcess process;
process.start("del /s *.txt");
// same as process.start("del", QStringList() << "/s" << "*.txt");
...
					

The program string can also contain quotes, to ensure that arguments containing spaces are correctly supplied to the new process. For example:

QProcess process;
process.start("dir \"My Documents\"");
					

QProcess object is already running a process, a warning may be printed at the console, and the existing process will continue running.

Note that, on Windows, quotes need to be both escaped and quoted. For example, the above code would be specified in the following way to ensure that "My Documents" is used as the argument to the dir executable:

QProcess process;
process.start("dir \"\"\"My Documents\"\"\"");
					

The OpenMode 被设为 mode .

[static] bool QProcess:: startDetached (const QString & program , const QStringList & arguments , const QString & workingDirectory , qint64 * pid = 0)

启动程序 program 采用自变量 arguments in a new process, and detaches from it. Returns true on success; otherwise returns false. If the calling process exits, the detached process will continue to live.

Note that arguments that contain spaces are not passed to the process as separate arguments.

Unix: 启动进程将在它自己的会话中运行,且行动像守护程序。

Windows: Arguments that contain spaces are wrapped in quotes. The started process will run as a regular standalone process.

进程将被启动在目录 workingDirectory .

注意: 在 QNX,这可能导致所有应用程序线程被临时冻结。

若函数成功,那么 * pid 被设为启动进程的进程标识符。

[static] bool QProcess:: startDetached (const QString & program , const QStringList & arguments )

启动程序 program 采用给定 arguments in a new process, and detaches from it. Returns true on success; otherwise returns false. If the calling process exits, the detached process will continue to live.

注意: Arguments that contain spaces are not passed to the process as separate arguments.

Unix: 启动进程将在它自己的会话中运行,且行动像守护程序。

Windows: Arguments that contain spaces are wrapped in quotes. The started process will run as a regular standalone process.

[static] bool QProcess:: startDetached (const QString & program )

这是重载函数。

启动程序 program 在新的进程中。 program is a single string of text containing both the program name and its arguments. The arguments are separated by one or more spaces.

The program string can also contain quotes, to ensure that arguments containing spaces are correctly supplied to the new process.

[signal] void QProcess:: started ()

此信号被发射由 QProcess 当此过程已开始,且 state () 返回 运行 .

QProcess::ProcessState QProcess:: state () const

返回进程的当前状态。

另请参阅 stateChanged () 和 error ().

[signal] void QProcess:: stateChanged ( QProcess::ProcessState newState )

此信号被发射每当状态为 QProcess 改变。 newState 自变量为状态 QProcess 要改变。

[static] QStringList QProcess:: systemEnvironment ()

以 key=value 对列表形式返回调用进程的环境。范例:

QStringList environment = QProcess::systemEnvironment();
// environment = {"PATH=/usr/bin:/usr/local/bin",
//                "USER=greg", "HOME=/home/greg"}
					

此函数不缓存系统环境。因此,获得环境的更新版本是可能的,若低级 C 库函数像 setenv ot putenv 有被调用。

不管怎样,注意,重复调用此函数将重新创建环境变量列表 (非通俗操作)。

注意: 对于新代码,推荐使用 QProcessEnvironment::systemEnvironment ()

该函数在 Qt 4.1 引入。

另请参阅 QProcessEnvironment::systemEnvironment (), environment (),和 setEnvironment ().

[slot] void QProcess:: terminate ()

试图终止进程。

进程可能不会因调用此函数而退出 (它有机会提示用户是否有未保存的文件,等等)。

On Windows, terminate() posts a WM_CLOSE message to all toplevel windows of the process and then to the main thread of the process itself. On Unix and Mac OS X the SIGTERM signal is sent.

不运行事件循环的 Windows 控制台应用程序 (或其事件循环不处理 WM_CLOSE 消息),只可被终止通过调用 kill ().

On Symbian, this function requires platform security capability PowerMgmt . If absent, the process will panic with KERN-EXEC 46.

注意: Terminating running processes from other processes will typically cause a panic in Symbian due to platform security.

另请参阅 Symbian Platform Security Requirements and kill ().

[虚拟] bool QProcess:: waitForBytesWritten ( int msecs = 30000)

重实现自 QIODevice::waitForBytesWritten ().

bool QProcess:: waitForFinished ( int msecs = 30000)

阻塞直到进程已完成且 finished () 信号已发射,或直到 msecs 毫秒已过去。

Returns true if the process finished; otherwise returns false (if the operation timed out, if an error occurred, or if this QProcess 已完成)。

可以在没有事件循环的情况下操作此函数。它很有用,当编写非 GUI 应用程序和在非 GUI 线程中履行 I/O 操作时。

警告: 从主 GUI 线程调用此函数可能导致用户界面被冻结。

若 msecs 为 -1,此函数不会超时。

另请参阅 finished (), waitForStarted (), waitForReadyRead (),和 waitForBytesWritten ().

[虚拟] bool QProcess:: waitForReadyRead ( int msecs = 30000)

重实现自 QIODevice::waitForReadyRead ().

bool QProcess:: waitForStarted ( int msecs = 30000)

阻塞直到进程启动且 started () 信号已发射,或直到 msecs 毫秒已过去。

Returns true if the process was started successfully; otherwise returns false (if the operation timed out or if an error occurred).

可以在没有事件循环的情况下操作此函数。它很有用,当编写非 GUI 应用程序和在非 GUI 线程中履行 I/O 操作时。

警告: 从主 GUI 线程调用此函数可能导致用户界面被冻结。

若 msecs 为 -1,此函数不会超时。

另请参阅 started (), waitForReadyRead (), waitForBytesWritten (),和 waitForFinished ().

QString QProcess:: workingDirectory () const

QProcess 已赋值工作目录,此函数返回工作目录 QProcess 将在程序开始之前进入。否则,返回空字符串 (即:没有赋值目录),且 QProcess 将使用应用程序的当前工作目录取而代之。

另请参阅 setWorkingDirectory ().

[virtual protected] qint64 QProcess:: writeData (const char * data , qint64 len )

重实现自 QIODevice::writeData ().

相关非成员

typedef Q_PID

Typedef for the identifiers used to represent processes on the underlying platform. On Unix and Symbian, this corresponds to qint64 ;在 Windows,它相当于 _PROCESS_INFORMATION* .

另请参阅 QProcess::pid ().