Qt 进程 QProcess
定義
The QProcess class is used to start external programs and to communicate with them.
QProcess類是用來(lái)啟動(dòng)一個(gè)外部程序并與之通信。
使用說(shuō)明
開啟外部線程的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().
想要啟動(dòng)一個(gè)進(jìn)程,可以傳遞你想啟動(dòng)的程序的名字和命令行參數(shù)作為參數(shù)傳遞給start()函數(shù)。參數(shù)作為QStringList中每一個(gè)單獨(dú)的字符串。
或者,你可以通過(guò)setProgram()和setArguments()來(lái)設(shè)置該程序,然后調(diào)用start()或者open()啟動(dòng)該程序。
啟動(dòng)時(shí)在指定程序名時(shí)就附帶上參數(shù)
QProcess process; process.start("cmd /c ping 127.0.0.1");- 1
- 2
啟動(dòng)時(shí)分別指定函數(shù)名和參數(shù)列表
QProcess process; process.start("cmd", QStringList()<<"/c"<<"ping 127.0.0.1", QIODevice::ReadWrite);- 1
- 2
分別指定函數(shù)名和參數(shù),然后再啟動(dòng)
QProcess process; process.setProgram("cmd"); process.setArguments(QStringList()<<"/c"<<"ping 127.0.0.1"); process.start(QIODevice::ReadWrite);- 1
- 2
- 3
- 4
一次性讀取進(jìn)程輸出
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();/** 如果打開的不是自動(dòng)關(guān)閉的進(jìn)程,那么這里最多可能會(huì)等待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()<<"======================"; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
按行讀取進(jìn)程輸出
void readProcessByLine() {qDebug()<<"readProcessByLine";qDebug()<<"----------------------";bool ret;QProcess process(0);/** cmd /c 的命令執(zhí)行完后會(huì)關(guān)閉窗口* cmd /k 的命令執(zhí)行完后不會(huì)關(guān)閉窗口** aa && bb 就是執(zhí)行aa,成功后再執(zhí)行bb* aa || bb 先執(zhí)行aa,若執(zhí)行成功則不再執(zhí)行bb,若失敗則執(zhí)行bb* a & b 表示執(zhí)行a再執(zhí)行b,而無(wú)論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) {/** 一個(gè)waitForReadyRead信號(hào)可能輸出的是多行*/ret = process.waitForReadyRead();qDebug()<<"waitForReadyRead"<<ret;if(!ret) {break;}while(true) {len = process.readLine(buffer, maxSize);qDebug()<<"buffer len"<<len;/** 因?yàn)槊恳恍兄辽龠€有回車換行符,因此讀到0,說(shuō)明waitForReadyRead超時(shí)返回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()<<"======================"; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
與進(jìn)程交互
void openProcessThenWrite() {qDebug()<<"openProcessThenWrite";qDebug()<<"----------------------";bool ret;QProcess *process = new QProcess(QThread::currentThread());/** 如果沒(méi)有QCoreApplication的話,會(huì)報(bào)如下錯(cuò)誤* QObject::startTimer: Timers can only be used with threads started with QThread. timer只能在同一個(gè)線程中創(chuàng)建和啟動(dòng).*/process->start("cmd", QIODevice::ReadWrite);ret = process->waitForStarted();qDebug()<<"waitForReadyRead"<<ret;QByteArray byteArray = process->readAllStandardOutput();QString str = QString::fromLocal8Bit(byteArray);qDebug()<<str;//這里不加換行的話命令發(fā)不出去qint64 len = process->write("ping 127.0.0.1\r\nexit\r\n");qDebug()<<"write len"<<len;while(true) {/** 如果進(jìn)程已經(jīng)關(guān)閉,waitForReadyRead會(huì)直接返回false*/ret = process->waitForReadyRead();qDebug()<<"waitForReadyRead"<<ret;if(!ret) {break;}byteArray = process->readAllStandardOutput();str = QString::fromLocal8Bit(byteArray);qDebug()<<str;qDebug()<<"";}/** 如果沒(méi)有關(guān)閉cmd的指令,因此該process永遠(yuǎn)不會(huì)finished,會(huì)超時(shí)(30秒)返回false*/ret = process->waitForFinished();qDebug()<<"waitForFinished"<<ret;process->close();delete process;qDebug()<<"";qDebug()<<"======================"; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
測(cè)試函數(shù)
#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(); }總結(jié)
以上是生活随笔為你收集整理的Qt 进程 QProcess的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Qt多线程学习:创建多线程
- 下一篇: QT5实现简单的TCP通信