定义

The QProcess class is used to start external programs and to communicate with them.

QProcess类是用来启动一个外部程序并与之通信。

使用说明

开启外部线程的3中方式

To start a process, pass the name and command line arguments of the program you want to run as arguments to start(). Arguments are supplied as individual strings in a QStringList.

Alternatively, you can set the program to run with setProgram() and setArguments(), and then call start() or open().

想要启动一个进程,可以传递你想启动的程序的名字和命令行参数作为参数传递给start()函数。参数作为QStringList中每一个单独的字符串。

或者,你可以通过setProgram()和setArguments()来设置该程序,然后调用start()或者open()启动该程序。

启动时在指定程序名时就附带上参数

QProcess process;
process.start("cmd /c ping 127.0.0.1");

启动时分别指定函数名和参数列表

QProcess process;
process.start("cmd", QStringList()<<"/c"<<"ping 127.0.0.1", QIODevice::ReadWrite);

分别指定函数名和参数,然后再启动

QProcess process;
process.setProgram("cmd");
process.setArguments(QStringList()<<"/c"<<"ping 127.0.0.1");
process.start(QIODevice::ReadWrite);

一次性读取进程输出

void readProcessAllOnce(bool processAutoExit) {
    qDebug()<<"readProcessAllOnce, processAutoExit ="<<processAutoExit;
    qDebug()<<"----------------------";
    bool ret;
    QProcess process;
    QString command;
    if(processAutoExit) {
        command = "cmd /c ping 127.0.0.1";
    } else {
        command = "cmd /k ping 127.0.0.1";
    }
    process.start(command);
    /*
     * bool QProcess::waitForStarted(int msecs = 30000)
     */
    ret = process.waitForStarted();
    qDebug()<<"waitForStarted"<<ret;
    qDebug()<<QDateTime::currentDateTime();
    /*
     * 如果打开的不是自动关闭的进程,那么这里最多可能会等待30秒
     * bool QProcess::waitForFinished(int msecs = 30000)
     */
    ret = process.waitForFinished();
    qDebug()<<QDateTime::currentDateTime();
    qDebug()<<"waitForFinished"<<ret;
    QByteArray byteArray = process.readAllStandardOutput();
    QString str = QString::fromLocal8Bit(byteArray);
    qDebug()<<str;
    process.close();
    qDebug()<<"";
    qDebug()<<"======================";
}

这里写图片描述

按行读取进程输出

void readProcessByLine() {
    qDebug()<<"readProcessByLine";
    qDebug()<<"----------------------";
    bool ret;
    QProcess process(0);
    /*
     * cmd /c 的命令执行完后会关闭窗口
     * cmd /k 的命令执行完后不会关闭窗口
     *
     * aa && bb 就是执行aa,成功后再执行bb
     * aa || bb 先执行aa,若执行成功则不再执行bb,若失败则执行bb
     * a  &  b  表示执行a再执行b,而无论a是否成功
     */
    process.start("cmd", QStringList()<<"/k"<<"ping 127.0.0.1"<<"&"<<"exit", QIODevice::ReadWrite);
//    process.setProgram("cmd");
//    process.setArguments(QStringList()<<"/k"<<"ping 127.0.0.1"<<"&"<<"exit");
//    process.start(QIODevice::ReadWrite);
    ret = process.waitForStarted();
    qDebug()<<"waitForStarted"<<ret;
    qint64 maxSize = 512;
    char buffer[maxSize];
    qint64 len;
    while(true) {
        /*
         * 一个waitForReadyRead信号可能输出的是多行
         */
        ret = process.waitForReadyRead();
        qDebug()<<"waitForReadyRead"<<ret;
        if(!ret) {
            break;
        }
        while(true) {
            len = process.readLine(buffer, maxSize);
            qDebug()<<"buffer len"<<len;
            /*
             * 因为每一行至少还有回车换行符,因此读到0,说明waitForReadyRead超时返回false
             */
            if(len <= 0) {
                break;
            }
            QString str = QString::fromLocal8Bit(buffer);
            qDebug()<<"qstring len"<<str.length();
            qDebug()<<str;
            qDebug()<<"";
        }
    }
    process.write("exit\r\n");
    ret = process.waitForFinished();
    qDebug()<<"waitForFinished"<<ret;
    process.close();
    qDebug()<<"";
    qDebug()<<"======================";
}

这里写图片描述

与进程交互

void openProcessThenWrite() {
    qDebug()<<"openProcessThenWrite";
    qDebug()<<"----------------------";
    bool ret;
    QProcess *process = new QProcess(QThread::currentThread());
    /*
     * 如果没有QCoreApplication的话,会报如下错误
     * QObject::startTimer: Timers can only be used with threads started with QThread. timer只能在同一个线程中创建和启动.
     */
    process->start("cmd", QIODevice::ReadWrite);
    ret = process->waitForStarted();
    qDebug()<<"waitForReadyRead"<<ret;
    QByteArray byteArray = process->readAllStandardOutput();
    QString str = QString::fromLocal8Bit(byteArray);
    qDebug()<<str;
    //这里不加换行的话命令发不出去
    qint64 len = process->write("ping 127.0.0.1\r\nexit\r\n");
    qDebug()<<"write len"<<len;
    while(true) {
        /*
         * 如果进程已经关闭,waitForReadyRead会直接返回false
         */
        ret = process->waitForReadyRead();
        qDebug()<<"waitForReadyRead"<<ret;
        if(!ret) {
            break;
        }
        byteArray = process->readAllStandardOutput();
        str = QString::fromLocal8Bit(byteArray);
        qDebug()<<str;
        qDebug()<<"";
    }
    /*
     * 如果没有关闭cmd的指令,因此该process永远不会finished,会超时(30秒)返回false
     */
    ret = process->waitForFinished();
    qDebug()<<"waitForFinished"<<ret;
    process->close();
    delete process;
    qDebug()<<"";
    qDebug()<<"======================";
}
  • 这里写图片描述

测试函数

#include <QCoreApplication>
#include <QDebug>
#include <QProcess>
#include <QThread>
#include <QDateTime>

void readProcessAllOnce(bool processAutoExit) {
    ……
}

void readProcessByLine() {
    ……
}

void openProcessThenWrite() {
    ……
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<<"======================";
    readProcessAllOnce(false);
    readProcessAllOnce(true);
    readProcessByLine();
    openProcessThenWrite();

    qDebug()<<"That's all";
    return a.exec();
}
posted on 2020-05-29 15:25  刘达人186  阅读(4229)  评论(0编辑  收藏  举报