⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qprocess.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    if (deathNotifier) {        deathNotifier->setEnabled(false);        delete deathNotifier;        deathNotifier = 0;    }    if (notifier) {        delete notifier;        notifier = 0;    }    destroyPipe(stdoutChannel.pipe);    destroyPipe(stderrChannel.pipe);    destroyPipe(stdinChannel.pipe);    destroyPipe(childStartedPipe);    destroyPipe(deathPipe);#ifdef Q_OS_UNIX    serial = 0;#endif}/*! \internal*/bool QProcessPrivate::_q_canReadStandardOutput(){    Q_Q(QProcess);    qint64 available = bytesAvailableFromStdout();    if (available == 0) {        if (stdoutChannel.notifier)            stdoutChannel.notifier->setEnabled(false);        destroyPipe(stdoutChannel.pipe);#if defined QPROCESS_DEBUG        qDebug("QProcessPrivate::canReadStandardOutput(), 0 bytes available");#endif        return false;    }    char *ptr = outputReadBuffer.reserve(available);    qint64 readBytes = readFromStdout(ptr, available);    if (readBytes == -1) {        processError = QProcess::ReadError;        q->setErrorString(QT_TRANSLATE_NOOP(QProcess, QLatin1String("Error reading from process")));        emit q->error(processError);#if defined QPROCESS_DEBUG        qDebug("QProcessPrivate::canReadStandardOutput(), failed to read from the process");#endif        return false;    }#if defined QPROCESS_DEBUG    qDebug("QProcessPrivate::canReadStandardOutput(), read %d bytes from the process' output",            int(readBytes));#endif    if (stdoutChannel.closed) {        outputReadBuffer.chop(readBytes);        return false;    }    outputReadBuffer.chop(available - readBytes);    bool didRead = false;    if (readBytes == 0) {        if (stdoutChannel.notifier)            stdoutChannel.notifier->setEnabled(false);    } else if (processChannel == QProcess::StandardOutput) {        didRead = true;        if (!emittedReadyRead) {            emittedReadyRead = true;            emit q->readyRead();            emittedReadyRead = false;        }    }    emit q->readyReadStandardOutput();    return didRead;}/*! \internal*/bool QProcessPrivate::_q_canReadStandardError(){    Q_Q(QProcess);    qint64 available = bytesAvailableFromStderr();    if (available == 0) {        if (stderrChannel.notifier)            stderrChannel.notifier->setEnabled(false);        destroyPipe(stderrChannel.pipe);        return false;    }    char *ptr = errorReadBuffer.reserve(available);    qint64 readBytes = readFromStderr(ptr, available);    if (readBytes == -1) {        processError = QProcess::ReadError;        q->setErrorString(QT_TRANSLATE_NOOP(QProcess, QLatin1String("Error reading from process")));        emit q->error(processError);        return false;    }    if (stderrChannel.closed) {        errorReadBuffer.chop(readBytes);        return false;    }    errorReadBuffer.chop(available - readBytes);    bool didRead = false;    if (readBytes == 0) {        if (stderrChannel.notifier)            stderrChannel.notifier->setEnabled(false);    } else if (processChannel == QProcess::StandardError) {        didRead = true;        if (!emittedReadyRead) {            emittedReadyRead = true;            emit q->readyRead();            emittedReadyRead = false;        }    }    emit q->readyReadStandardError();    return didRead;}/*! \internal*/bool QProcessPrivate::_q_canWrite(){    Q_Q(QProcess);    if (stdinChannel.notifier)        stdinChannel.notifier->setEnabled(false);    if (writeBuffer.isEmpty()) {#if defined QPROCESS_DEBUG        qDebug("QProcessPrivate::canWrite(), not writing anything (empty write buffer).");#endif        return false;    }    qint64 written = writeToStdin(writeBuffer.readPointer(),                                      writeBuffer.nextDataBlockSize());    if (written < 0) {        destroyPipe(stdinChannel.pipe);        processError = QProcess::WriteError;        q->setErrorString(QT_TRANSLATE_NOOP(QProcess, QLatin1String("Error writing to process")));#if defined QPROCESS_DEBUG        qDebug("QProcessPrivate::canWrite(), failed to write (%s)", strerror(errno));#endif        emit q->error(processError);        return false;    }#if defined QPROCESS_DEBUG    qDebug("QProcessPrivate::canWrite(), wrote %d bytes to the process input", int(written));#endif    writeBuffer.free(written);    if (!emittedBytesWritten) {        emittedBytesWritten = true;        emit q->bytesWritten(written);        emittedBytesWritten = false;    }    if (stdinChannel.notifier && !writeBuffer.isEmpty())        stdinChannel.notifier->setEnabled(true);    if (writeBuffer.isEmpty() && stdinChannel.closed)        closeWriteChannel();    return true;}/*! \internal*/bool QProcessPrivate::_q_processDied(){    Q_Q(QProcess);#if defined QPROCESS_DEBUG    qDebug("QProcessPrivate::_q_processDied()");#endif#ifdef Q_OS_UNIX    if (!waitForDeadChild())        return false;#endif#ifdef Q_OS_WIN    if (processFinishedNotifier)        processFinishedNotifier->setEnabled(false);#endif    // the process may have died before it got a chance to report that it was    // either running or stopped, so we will call _q_startupNotification() and    // give it a chance to emit started() or error(FailedToStart).    if (processState == QProcess::Starting) {        if (!_q_startupNotification())            return true;    }    if (dying) {        // at this point we know the process is dead. prevent        // reentering this slot recursively by calling waitForFinished()        // or opening a dialog inside slots connected to the readyRead        // signals emitted below.        return true;    }    dying = true;        // in case there is data in the pipe line and this slot by chance    // got called before the read notifications, call these two slots    // so the data is made available before the process dies.    _q_canReadStandardOutput();    _q_canReadStandardError();    findExitCode();    if (crashed) {        exitStatus = QProcess::CrashExit;        processError = QProcess::Crashed;        q->setErrorString(QT_TRANSLATE_NOOP(QProcess, QLatin1String("Process crashed")));        emit q->error(processError);    }    bool wasRunning = (processState == QProcess::Running);    cleanup();    processState = QProcess::NotRunning;    emit q->stateChanged(processState);    if (wasRunning) {        emit q->finished(exitCode);        emit q->finished(exitCode, exitStatus);    }#if defined QPROCESS_DEBUG    qDebug("QProcessPrivate::_q_processDied() process is dead");#endif    return true;}/*! \internal*/bool QProcessPrivate::_q_startupNotification(){    Q_Q(QProcess);#if defined QPROCESS_DEBUG    qDebug("QProcessPrivate::startupNotification()");#endif    if (startupSocketNotifier)        startupSocketNotifier->setEnabled(false);    if (processStarted()) {        processState = QProcess::Running;        emit q->started();        return true;    }    processState = QProcess::NotRunning;    processError = QProcess::FailedToStart;    emit q->error(processError);#ifdef Q_OS_UNIX    // make sure the process manager removes this entry    waitForDeadChild();    findExitCode();#endif    cleanup();    return false;}/*! \internal*/void QProcessPrivate::closeWriteChannel(){#if defined QPROCESS_DEBUG    qDebug("QProcessPrivate::closeWriteChannel()");#endif    if (stdinChannel.notifier) {        stdinChannel.notifier->setEnabled(false);        delete stdinChannel.notifier;        stdinChannel.notifier = 0;    }#ifdef Q_OS_WIN    // ### Find a better fix, feeding the process little by little    // instead.    flushPipeWriter();#endif    destroyPipe(stdinChannel.pipe);}/*!    Constructs a QProcess object with the given \a parent.*/QProcess::QProcess(QObject *parent)    : QIODevice(*new QProcessPrivate, parent){#if defined QPROCESS_DEBUG    qDebug("QProcess::QProcess(%p)", parent);#endif}/*!    Destructs the QProcess object, i.e., killing the process.    Note that this function will not return until the process is    terminated.*/QProcess::~QProcess(){    Q_D(QProcess);    if (d->processState != NotRunning) {        qWarning("QProcess: Destroyed while process is still running.");        kill();        waitForFinished();    }#ifdef Q_OS_UNIX    // make sure the process manager removes this entry    d->findExitCode();#endif    d->cleanup();}/*!    \obsolete    Returns the read channel mode of the QProcess. This function is    equivalent to processChannelMode()    \sa processChannelMode()*/QProcess::ProcessChannelMode QProcess::readChannelMode() const{    return processChannelMode();}/*!    \obsolete    Use setProcessChannelMode(\a mode) instead.    \sa setProcessChannelMode()*/void QProcess::setReadChannelMode(ProcessChannelMode mode){    setProcessChannelMode(mode);}/*!    \since 4.2    Returns the channel mode of the QProcess standard output and    standard error channels.    \sa setReadChannelMode(), ProcessChannelMode, setReadChannel()*/QProcess::ProcessChannelMode QProcess::processChannelMode() const{    Q_D(const QProcess);    return d->processChannelMode;}/*!    \since 4.2    Sets the channel mode of the QProcess standard output and standard    error channels to the \a mode specified.    This mode will be used the next time start() is called. For example:    \code        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();    \endcode    \sa readChannelMode(), ProcessChannelMode, setReadChannel()*/void QProcess::setProcessChannelMode(ProcessChannelMode mode){    Q_D(QProcess);    d->processChannelMode = mode;}/*!    Returns the current read channel of the QProcess.    \sa setReadChannel()*/QProcess::ProcessChannel QProcess::readChannel() const{    Q_D(const QProcess);    return d->processChannel;}/*!    Sets the current read channel of the QProcess to the given \a    channel. The current input channel is used by the functions    read(), readAll(), readLine(), and getChar(). It also determines    which channel triggers QProcess to emit readyRead().    \sa readChannel()*/void QProcess::setReadChannel(ProcessChannel channel){    Q_D(QProcess);    if (d->processChannel != channel) {        QByteArray buf = d->buffer.readAll();        if (d->processChannel == QProcess::StandardOutput) {            for (int i = buf.size() - 1; i >= 0; --i)                d->outputReadBuffer.ungetChar(buf.at(i));        } else {            for (int i = buf.size() - 1; i >= 0; --i)                d->errorReadBuffer.ungetChar(buf.at(i));        }    }    d->processChannel = channel;}/*!    Closes the read channel \a channel. After calling this function,    QProcess will no longer receive data on the channel. Any data that    has already been received is still available for reading.    Call this function to save memory, if you are not interested in    the output of the process.    \sa closeWriteChannel(), setReadChannel()*/void QProcess::closeReadChannel(ProcessChannel channel){    Q_D(QProcess);    if (channel == StandardOutput)        d->stdoutChannel.closed = true;    else        d->stderrChannel.closed = true;}/*!    Schedules the write channel of QProcess to be closed. The channel    will close once all data has been written to the process. After    calling this function, any attempts to write to the process will    fail.    Closing the write channel is necessary for programs that read    input data until the channel has been closed. For example, the    program "more" is used to display text data in a console on both    Unix and Windows. But it will not display the text data until    QProcess's write channel has been closed. Example:    \code        QProcess more;        more.start("more");        more.write("Text to display");        more.closeWriteChannel();        // QProcess will emit readyRead() once "more" starts printing    \endcode    The write channel is implicitly opened when start() is called.    \sa closeReadChannel()

⌨️ 快捷键说明

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