定义
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) ret = process.waitForStarted() qDebug()<<"waitForStarted"<<ret qDebug()<<QDateTime::currentDateTime() 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
按行读取进程输出
void readProcessByLine() { qDebug()<<"readProcessByLine" qDebug()<<"----------------------" bool ret QProcess process(0) 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) { ret = process.waitForReadyRead() qDebug()<<"waitForReadyRead"<<ret if(!ret) { break } while(true) { len = process.readLine(buffer, maxSize) qDebug()<<"buffer len"<<len 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
与进程交互
void openProcessThenWrite() { qDebug()<<"openProcessThenWrite"; qDebug()<<"----------------------"; bool ret; QProcess *process = new QProcess(QThread::currentThread()); 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) { ret = process->waitForReadyRead(); qDebug()<<"waitForReadyRead"<<ret; if(!ret) { break; } byteArray = process->readAllStandardOutput(); str = QString::fromLocal8Bit(byteArray); qDebug()<<str; qDebug()<<""; } 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
测试函数
#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();}