📄 q3process.cpp
字号:
}/*! 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. If normalExit() is false (e.g. if the program was killed or crashed), this function returns 0, so you should check the return value of normalExit() before relying on this value. \sa normalExit() processExited()*/int Q3Process::exitStatus() const{ // isRunning() has the side effect that it determines the exit status! if ( isRunning() ) return 0; else return exitStat;}/*! Reads the data that the process has written to standard output. When new data is written to standard output, the class emits the signal readyReadStdout(). If there is no data to read, this function returns a QByteArray of size 0: it does not wait until there is something to read. \sa readyReadStdout() readLineStdout() readStderr() writeToStdin()*/QByteArray Q3Process::readStdout(){ if ( readStdoutCalled ) { return QByteArray(); } readStdoutCalled = true; Q3Membuf *buf = membufStdout(); readStdoutCalled = false; return buf->readAll();}/*! Reads the data that the process has written to standard error. When new data is written to standard error, the class emits the signal readyReadStderr(). If there is no data to read, this function returns a QByteArray of size 0: it does not wait until there is something to read. \sa readyReadStderr() readLineStderr() readStdout() writeToStdin()*/QByteArray Q3Process::readStderr(){ if ( readStderrCalled ) { return QByteArray(); } readStderrCalled = true; Q3Membuf *buf = membufStderr(); readStderrCalled = false; return buf->readAll();}/*! Reads a line of text from standard output, excluding any trailing newline or carriage return characters, and returns it. Returns an empty string if canReadLineStdout() 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 with QTextCodec::setCodecForCStrings(). \sa canReadLineStdout() readyReadStdout() readStdout() readLineStderr()*/QString Q3Process::readLineStdout(){ QByteArray a( 256 ); Q3Membuf *buf = membufStdout(); if ( !buf->scanNewline( &a ) ) { if ( !canReadLineStdout() ) return QString(); if ( !buf->scanNewline( &a ) ) return QString( QLatin1String(buf->readAll()) ); } uint size = a.size(); buf->consumeBytes( size, 0 ); // get rid of terminating \n or \r\n if ( size>0 && a.at( size - 1 ) == '\n' ) { if ( size>1 && a.at( size - 2 ) == '\r' ) a.chop(2); else a.chop(1); } return QString(QString::fromLatin1(a.constData()));}/*! Reads a line of text from standard error, excluding any trailing newline or carriage return characters and returns it. Returns an empty string if canReadLineStderr() 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 with QTextCodec::setCodecForCStrings(). \sa canReadLineStderr() readyReadStderr() readStderr() readLineStdout()*/QString Q3Process::readLineStderr(){ QByteArray a( 256 ); Q3Membuf *buf = membufStderr(); if ( !buf->scanNewline( &a ) ) { if ( !canReadLineStderr() ) return QString(); if ( !buf->scanNewline( &a ) ) return QString( QString::fromLatin1( buf->readAll().constData() ) ); } uint size = a.size(); buf->consumeBytes( size, 0 ); // get rid of terminating \n or \r\n if ( size>0 && a.at( size - 1 ) == '\n' ) { if ( size>1 && a.at( size - 2 ) == '\r' ) a.chop(2); else a.chop(1); } return QString( QString::fromLatin1( a.constData() ) );}/*! \fn void Q3Process::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. This signal is especially useful if you want to know when you can safely delete the Q3Process object when you are not interested in reading from standard output or standard error. \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 string list 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 Q3Process::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 Q3Process::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 Q3Process::closeStdinLaunch(){ disconnect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); closeStdin(); emit launchFinished();}/*! \fn void Q3Process::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 Q3Process::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 Q3Process::processExited() This signal is emitted when the process has exited. \sa isRunning() normalExit() exitStatus() start() launch()*//*! \fn void Q3Process::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 Q3Process::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 Q3Process::connectNotify( const char * signal ){#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::connectNotify(): signal %s has been connected", signal );#endif if ( !ioRedirection ) if ( qstrcmp( signal, SIGNAL(readyReadStdout()) )==0 || qstrcmp( signal, SIGNAL(readyReadStderr()) )==0 ) {#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::connectNotify(): set ioRedirection to true" );#endif setIoRedirection( true ); return; } if ( !notifyOnExit && qstrcmp( signal, SIGNAL(processExited()) )==0 ) {#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::connectNotify(): set notifyOnExit to true" );#endif setNotifyOnExit( true ); return; } if ( !wroteToStdinConnected && qstrcmp( signal, SIGNAL(wroteToStdin()) )==0 ) {#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::connectNotify(): set wroteToStdinConnected to true" );#endif setWroteStdinConnected( true ); return; }}/*! \reimp*/void Q3Process::disconnectNotify( const char * ){ if ( ioRedirection && receivers( SIGNAL(readyReadStdout()) ) ==0 && receivers( SIGNAL(readyReadStderr()) ) ==0 ) {#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::disconnectNotify(): set ioRedirection to false" );#endif setIoRedirection( false ); } if ( notifyOnExit && receivers( SIGNAL(processExited()) ) == 0 ) {#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::disconnectNotify(): set notifyOnExit to false" );#endif setNotifyOnExit( false ); } if ( wroteToStdinConnected && receivers( SIGNAL(wroteToStdin()) ) == 0 ) {#if defined(QT_Q3PROCESS_DEBUG) qDebug( "Q3Process::disconnectNotify(): set wroteToStdinConnected to false" );#endif setWroteStdinConnected( false ); }}#endif // QT_NO_PROCESS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -