qprocess.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 595 行 · 第 1/3 页

HTML
595
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/src/kernel/qprocess.cpp:51 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>QProcess Class</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QProcess Class Reference</h1><p>The QProcess class is used to start external programs andto communicate with them.<a href="#details">More...</a><p><tt>#include &lt;<a href="qprocess-h.html">qprocess.h</a>&gt;</tt><p>Inherits <a href="qobject.html">QObject</a>.<p><a href="qprocess-members.html">List of all member functions.</a><h2>Public Members</h2><ul><li><div class=fn><a href="#QProcess"><b>QProcess</b></a> ( QObject&nbsp;*&nbsp;parent = 0, const&nbsp;char&nbsp;*&nbsp;name = 0 )</div></li><li><div class=fn><a href="#QProcess-2"><b>QProcess</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;arg0, QObject&nbsp;*&nbsp;parent = 0, const&nbsp;char&nbsp;*&nbsp;name = 0 )</div></li><li><div class=fn><a href="#QProcess-3"><b>QProcess</b></a> ( const&nbsp;QStringList&nbsp;&amp;&nbsp;args, QObject&nbsp;*&nbsp;parent = 0, const&nbsp;char&nbsp;*&nbsp;name = 0 )</div></li><li><div class=fn><a href="#~QProcess"><b>~QProcess</b></a> ()</div></li><li><div class=fn>QStringList <a href="#arguments"><b>arguments</b></a> () const</div></li><li><div class=fn>void <a href="#clearArguments"><b>clearArguments</b></a> ()</div></li><li><div class=fn>virtual void <a href="#setArguments"><b>setArguments</b></a> ( const&nbsp;QStringList&nbsp;&amp;&nbsp;args )</div></li><li><div class=fn>virtual void <a href="#addArgument"><b>addArgument</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;arg )</div></li><li><div class=fn>QDir <a href="#workingDirectory"><b>workingDirectory</b></a> () const</div></li><li><div class=fn>virtual void <a href="#setWorkingDirectory"><b>setWorkingDirectory</b></a> ( const&nbsp;QDir&nbsp;&amp;&nbsp;dir )</div></li><li><div class=fn>enum <a href="#Communication-enum"><b>Communication</b></a> { Stdin = 0x01, Stdout = 0x02, Stderr = 0x04, DupStderr = 0x08 }</div></li><li><div class=fn>void <a href="#setCommunication"><b>setCommunication</b></a> ( int&nbsp;commFlags )</div></li><li><div class=fn>int <a href="#communication"><b>communication</b></a> () const</div></li><li><div class=fn>virtual bool <a href="#start"><b>start</b></a> ( QStringList&nbsp;*&nbsp;env = 0 )</div></li><li><div class=fn>virtual bool <a href="#launch-2"><b>launch</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;buf, QStringList&nbsp;*&nbsp;env = 0 )</div></li><li><div class=fn>virtual bool <a href="#launch"><b>launch</b></a> ( const&nbsp;QByteArray&nbsp;&amp;&nbsp;buf, QStringList&nbsp;*&nbsp;env = 0 )</div></li><li><div class=fn>bool <a href="#isRunning"><b>isRunning</b></a> () const</div></li><li><div class=fn>bool <a href="#normalExit"><b>normalExit</b></a> () const</div></li><li><div class=fn>int <a href="#exitStatus"><b>exitStatus</b></a> () const</div></li><li><div class=fn>virtual QByteArray <a href="#readStdout"><b>readStdout</b></a> ()</div></li><li><div class=fn>virtual QByteArray <a href="#readStderr"><b>readStderr</b></a> ()</div></li><li><div class=fn>bool <a href="#canReadLineStdout"><b>canReadLineStdout</b></a> () const</div></li><li><div class=fn>bool <a href="#canReadLineStderr"><b>canReadLineStderr</b></a> () const</div></li><li><div class=fn>virtual QString <a href="#readLineStdout"><b>readLineStdout</b></a> ()</div></li><li><div class=fn>virtual QString <a href="#readLineStderr"><b>readLineStderr</b></a> ()</div></li><li><div class=fn>PID <a href="#processIdentifier"><b>processIdentifier</b></a> ()</div></li></ul><h2>Public Slots</h2><ul><li><div class=fn>void <a href="#tryTerminate"><b>tryTerminate</b></a> () const</div></li><li><div class=fn>void <a href="#kill"><b>kill</b></a> () const</div></li><li><div class=fn>virtual void <a href="#writeToStdin"><b>writeToStdin</b></a> ( const&nbsp;QByteArray&nbsp;&amp;&nbsp;buf )</div></li><li><div class=fn>virtual void <a href="#writeToStdin-2"><b>writeToStdin</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;buf )</div></li><li><div class=fn>virtual void <a href="#closeStdin"><b>closeStdin</b></a> ()</div></li></ul><h2>Signals</h2><ul><li><div class=fn>void <a href="#readyReadStdout"><b>readyReadStdout</b></a> ()</div></li><li><div class=fn>void <a href="#readyReadStderr"><b>readyReadStderr</b></a> ()</div></li><li><div class=fn>void <a href="#processExited"><b>processExited</b></a> ()</div></li><li><div class=fn>void <a href="#wroteToStdin"><b>wroteToStdin</b></a> ()</div></li><li><div class=fn>void <a href="#launchFinished"><b>launchFinished</b></a> ()</div></li></ul><hr><a name="details"></a><h2>Detailed Description</h2><p> The QProcess class is used to start external programs andto communicate with them.<p> <p> You can write to the started program's standard input, and canread the program's standard output and standard error. You canpass command line arguments to the program either in theconstructor or with <a href="#setArguments">setArguments</a>() or <a href="#addArgument">addArgument</a>(). The program'sworking directory can be set with <a href="#setWorkingDirectory">setWorkingDirectory</a>(). If youneed to set up environment variables pass them to the <a href="#start">start</a>() or<a href="#launch">launch</a>() functions (see below). The <a href="#processExited">processExited</a>() signal isemitted if the program exits. The program's exit status isavailable from <a href="#exitStatus">exitStatus</a>(), although you could simply call<a href="#normalExit">normalExit</a>() to see if the program terminated normally.<p> There are two different ways to start a process. If you just wantto run a program, optionally passing data to its standard input atthe beginning, use one of the launch() functions. If you want fullcontrol of the program's standard input (especially if you don'tknow all the data you want to send to standard input at thebeginning), use the start() function.<p> If you use start() you can write to the program's standard inputusing <a href="#writeToStdin">writeToStdin</a>() and you can close the standard input with<a href="#closeStdin">closeStdin</a>(). The <a href="#wroteToStdin">wroteToStdin</a>() signal is emitted if the datasent to standard input has been written. You can read from theprogram's standard output using <a href="#readStdout">readStdout</a>() or <a href="#readLineStdout">readLineStdout</a>().These functions return an empty <a href="qbytearray.html">QByteArray</a> if there is no data toread. The <a href="#readyReadStdout">readyReadStdout</a>() signal is emitted when there is dataavailable to be read from standard output. Standard error has aset of functions that correspond to the standard output functions,i.e. <a href="#readStderr">readStderr</a>(), <a href="#readLineStderr">readLineStderr</a>() and <a href="#readyReadStderr">readyReadStderr</a>().<p> If you use one of the <a href="#launch">launch</a>() functions the data you pass will besent to the program's standard input which will be closed once allthe data has been written. You should <em>not</em> use <a href="#writeToStdin">writeToStdin</a>() or<a href="#closeStdin">closeStdin</a>() if you use launch(). If you need to send data to theprogram's standard input after it has started running use <a href="#start">start</a>()instead of launch().<p> Both start() and launch() can accept a string list of strings eachof which has the format, key=value, where the keys are the namesof environment variables.<p> You can test to see if a program is running with <a href="#isRunning">isRunning</a>(). Theprogram's process identifier is available from<a href="#processIdentifier">processIdentifier</a>(). If you want to terminate a running programuse <a href="#tryTerminate">tryTerminate</a>(), but note that the program may ignore this. Ifyou <em>really</em> want to terminate the program, without it having anychance to clean up, you can use <a href="#kill">kill</a>().<p> As an example, suppose we want to start the <tt>uic</tt> command (a Qtcommand line tool used with <em>Qt Designer</em>) and perform someoperations on the output (the <tt>uic</tt> outputs the code it generatesto standard output by default). Suppose further that we want torun the program on the file "small_dialog.ui" with the commandline options "-tr i18n". On the command line we would write:<pre>    uic -tr i18n small_dialog.ui    </pre> <p> <p> A code snippet for this with the QProcess class might look likethis:<p> <pre>    UicManager::UicManager()    {</pre><pre>        proc = new QProcess( this );</pre><pre>    <a name="x2093"></a>    proc-&gt;<a href="#addArgument">addArgument</a>( "uic" );        proc-&gt;<a href="#addArgument">addArgument</a>( "-tr" );        proc-&gt;<a href="#addArgument">addArgument</a>( "i18n" );        proc-&gt;<a href="#addArgument">addArgument</a>( "small_dialog.ui" );    <a name="x2094"></a>    <a href="qobject.html#connect">connect</a>( proc, SIGNAL(<a href="#readyReadStdout">readyReadStdout</a>()),                this, SLOT(readFromStdout()) );</pre><pre>    <a name="x2095"></a>    if ( !proc-&gt;<a href="#start">start</a>() ) {            // error handling</pre><pre>        }    }</pre><p> <pre>    void UicManager::readFromStdout()    {        // Read and process the data.        // Bear in mind that the data might be output in chunks.</pre><pre>    }</pre><p> 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 forarguments passed to <a href="#addArgument">addArgument</a>() or <a href="#setArguments">setArguments</a>().<p> The <a href="#readyReadStdout">readyReadStdout</a>() signal is emitted when there is new data onstandard output. This happens asynchronously: you don't know ifmore data will arrive later. In the above example you couldconnect the <a href="#processExited">processExited</a>() signal to the slotUicManager::readFromStdout() instead. If you do so, you will becertain that all the data is available when the slot is called. Onthe other hand, you must wait until the process has finishedbefore doing any processing.<p> <p>See also <a href="qsocket.html">QSocket</a>, <a href="io.html">Input/Output and Networking</a> and <a href="misc.html">Miscellaneous Classes</a>.<hr><h2>Member Type Documentation</h2><h3 class=fn><a name="Communication-enum"></a>QProcess::Communication</h3><p> This enum type defines the communication channels connected to theprocess.<ul><li><tt>QProcess::Stdin</tt> - Data can be written to the process's standard input.<li><tt>QProcess::Stdout</tt> - Data can be read from the process's standardoutput.<li><tt>QProcess::Stderr</tt> - Data can be read from the process's standard error.<li><tt>QProcess::DupStderr</tt> - Both the process's standard error output <em>and</em>its standard output are written to its standard output. (LikeUnix's dup2().) This means that nothing is sent to the standarderror output. This is especially useful if your applicationrequires that the output on standard output and on standard errormust be read in the same order that they are produced. This is aflag, so to activate it you must pass <tt>Stdout|Stderr|DupStderr</tt>,or <tt>Stdin|Stdout|Stderr|DupStderr</tt> if you want to provide input,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?