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