📄 qprocess.cpp
字号:
*/bool QProcess::canReadLineStdout() const{ QProcess *that = (QProcess*)this; return that->scanNewline( TRUE, 0 );}/*! Returns TRUE if it's possible to read an entire line of text from standard error at this time; otherwise returns FALSE. \sa readLineStderr() canReadLineStdout()*/bool QProcess::canReadLineStderr() const{ QProcess *that = (QProcess*)this; return that->scanNewline( FALSE, 0 );}/*! 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. \sa canReadLineStdout() readyReadStdout() readStdout() readLineStderr()*/QString QProcess::readLineStdout(){ QByteArray a; QString s; if ( scanNewline( TRUE, &a ) ) { if ( a.isEmpty() ) s = ""; else s = QString( a ); } return s;}/*! 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. \sa canReadLineStderr() readyReadStderr() readStderr() readLineStdout()*/QString QProcess::readLineStderr(){ QByteArray a; QString s; if ( scanNewline( FALSE, &a ) ) { if ( a.isEmpty() ) s = ""; else s = QString( a ); } return s;}/*! This private function scans for any occurrence of \n or \r\n in the buffer \e buf. It stores the text in the byte array \a store if it is non-null.*/bool QProcess::scanNewline( bool stdOut, QByteArray *store ){ QByteArray *buf; if ( stdOut ) buf = bufStdout(); else buf = bufStderr(); uint n = buf->size(); uint i; for ( i=0; i<n; i++ ) { if ( buf->at(i) == '\n' ) { break; } } if ( i >= n ) return FALSE; if ( store ) { uint lineLength = i; if ( lineLength>0 && buf->at(lineLength-1) == '\r' ) lineLength--; // (if there are two \r, let one stay) store->resize( lineLength ); memcpy( store->data(), buf->data(), lineLength ); if ( stdOut ) consumeBufStdout( i+1 ); else consumeBufStderr( i+1 ); } return TRUE;}/*! \fn void QProcess::launchFinished() 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. \sa launch() QObject::deleteLater()*//*! Runs the process and writes the data \a 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 \a env is null, then the process is started with the same environment as the starting process. If \a env is non-null, then the values in the stringlist are interpreted as environment setttings of the form \c {key=value} and the process is started with these environment settings. For convenience, there is a small exception to this rule under Unix: if \a env does not contain any settings for the environment variable \c 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 \a 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. \sa start() launchFinished();*/bool QProcess::launch( const QByteArray& buf, QStringList *env ){ if ( start( env ) ) { if ( !buf.isEmpty() ) { connect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); writeToStdin( buf ); } else { closeStdin(); emit launchFinished(); } return TRUE; } else { emit launchFinished(); return FALSE; }}/*! \overload The data \a buf is written to standard input with writeToStdin() using the QString::local8Bit() representation of the strings.*/bool QProcess::launch( const QString& buf, QStringList *env ){ if ( start( env ) ) { if ( !buf.isEmpty() ) { connect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); writeToStdin( buf ); } else { closeStdin(); emit launchFinished(); } return TRUE; } else { emit launchFinished(); return FALSE; }}/*! This private slot is used by the launch() functions to close standard input.*/void QProcess::closeStdinLaunch(){ disconnect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); closeStdin(); emit launchFinished();}/*! \fn void QProcess::readyReadStdout() 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. \sa readStdout() readLineStdout() readyReadStderr()*//*! \fn void QProcess::readyReadStderr() 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. \sa readStderr() readLineStderr() readyReadStdout()*//*! \fn void QProcess::processExited() This signal is emitted when the process has exited. \sa isRunning() normalExit() exitStatus() start() launch()*//*! \fn void QProcess::wroteToStdin() 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. \sa writeToStdin() closeStdin()*//*! \overload The string \a buf is handled as text using the QString::local8Bit() representation.*/void QProcess::writeToStdin( const QString& buf ){ QByteArray tmp = buf.local8Bit(); tmp.resize( buf.length() ); writeToStdin( tmp );}/* * Under Windows the implementation is not so nice: it is not that easy to * detect when one of the signals should be emitted; therefore there are some * timers that query the information. * To keep it a little efficient, use the timers only when they are needed. * They are needed, if you are interested in the signals. So use * connectNotify() and disconnectNotify() to keep track of your interest. *//*! \reimp*/void QProcess::connectNotify( const char * signal ){#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::connectNotify(): signal %s has been connected", signal );#endif if ( !ioRedirection ) if ( qstrcmp( signal, SIGNAL(readyReadStdout()) )==0 || qstrcmp( signal, SIGNAL(readyReadStderr()) )==0 ) {#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::connectNotify(): set ioRedirection to TRUE" );#endif setIoRedirection( TRUE ); return; } if ( !notifyOnExit && qstrcmp( signal, SIGNAL(processExited()) )==0 ) {#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::connectNotify(): set notifyOnExit to TRUE" );#endif setNotifyOnExit( TRUE ); return; } if ( !wroteToStdinConnected && qstrcmp( signal, SIGNAL(wroteToStdin()) )==0 ) {#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::connectNotify(): set wroteToStdinConnected to TRUE" );#endif setWroteStdinConnected( TRUE ); return; }}/*! \reimp*/void QProcess::disconnectNotify( const char * ){ if ( ioRedirection && receivers( SIGNAL(readyReadStdout()) ) ==0 && receivers( SIGNAL(readyReadStderr()) ) ==0 ) {#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::disconnectNotify(): set ioRedirection to FALSE" );#endif setIoRedirection( FALSE ); } if ( notifyOnExit && receivers( SIGNAL(processExited()) ) == 0 ) {#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::disconnectNotify(): set notifyOnExit to FALSE" );#endif setNotifyOnExit( FALSE ); } if ( wroteToStdinConnected && receivers( SIGNAL(wroteToStdin()) ) == 0 ) {#if defined(QT_QPROCESS_DEBUG) qDebug( "QProcess::disconnectNotify(): set wroteToStdinConnected to FALSE" );#endif setWroteStdinConnected( FALSE ); }}#endif // QT_NO_PROCESS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -