qt qprocess执行命令行
转自:https://www.pudn.com/news/62a88a12a11cf7345f9b03d8.html
由于工作需求,要使用QT执行cmd命令。鉴于网上的方法实现了适合自己的一种方法。
先放效果图
QProcess Class
可用于完成启动外部程序,并与之交互通信。
创建并启动
在 QProcess 中,输入通道分为标准错误通道和标准输出通道,对应的信号分别是 readyReadStandardError() 和 readyRead()。
可通过 setProcessChannelMode(QProcess::MergedChannels) 将标准错误通道合并至标准输出通道。
process = new QProcess(this);
process->setProcessChannelMode(QProcess::MergedChannels);
...
process->start("cmd.exe");
Signal
readyRead() 将我们输入的指令显示到控制台窗口中。
readyReadStandardOutput()将 cmd 指令返回结果显示至控制台窗口中。
errorOccurred(QProcess::ProcessError)将 QProcess 发送的打印至控制台。
finished(int)当 QProcess 退出时,mProcess 回到 NotRunning 状态,并发出 finished() 信号。
源码
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QProcess> 6 #include <handleapi.h> 7 #include <QShortcut> 8 9 namespace Ui { 10 class MainWindow; 11 } 12 13 class MainWindow : public QMainWindow 14 { 15 Q_OBJECT 16 17 public: 18 explicit MainWindow(QWidget *parent = 0); 19 ~MainWindow(); 20 21 private: 22 Ui::MainWindow *ui; 23 QProcess *process; 24 QShortcut *key; 25 26 public slots: 27 void write_cmd(); 28 void read_data(); 29 void finished_process(); 30 void error_process(); 31 32 }; 33 34 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 #include <QDebug> 5 #include <QShortcut> 6 7 MainWindow::MainWindow(QWidget *parent) : 8 QMainWindow(parent), 9 ui(new Ui::MainWindow) 10 { 11 setWindowTitle("cmd text"); 12 13 ui->setupUi(this); 14 ui->lineEdit->setText(tr("ping zhangmengmm.top")); 15 ui->textEdit->setLineWrapMode(QTextEdit::NoWrap); 16 17 process = new QProcess(this); 18 process->setProcessChannelMode(QProcess::MergedChannels); 19 20 ui->pushButton_Enter->setFocus(); 21 ui->pushButton_Enter->setShortcut(QKeySequence::InsertParagraphSeparator); 22 ui->pushButton_Enter->setShortcut(Qt::Key_Enter); 23 ui->pushButton_Enter->setShortcut(Qt::Key_Return); 24 25 connect(ui->pushButton,SIGNAL(clicked()),ui->lineEdit,SLOT(clear())); //清除命令行 26 connect(ui->pushButton_Enter,SIGNAL(clicked()),this,SLOT(write_cmd())); //回车按钮 27 connect(ui->pushButton_Clear,SIGNAL(clicked()),ui->textEdit,SLOT(clear())); //清除文本框按钮 28 29 connect(process,SIGNAL(readyRead()),this,SLOT(read_data())); //读命令行数据 30 connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(read_data())); //读命令行标准数据(兼容) 31 connect(process,SIGNAL(errorOccurred(QProcess::ProcessError)),this,SLOT(error_process())); //命令行错误处理 32 connect(process,SIGNAL(finished(int)),this,SLOT(finished_process())); //命令行结束处理 33 34 connect(ui->pushButton,SIGNAL(clicked()),ui->lineEdit,SLOT(setFocus())); 35 connect(ui->pushButton_Enter,SIGNAL(clicked()),ui->lineEdit,SLOT(setFocus())); 36 connect(ui->pushButton_Clear,SIGNAL(clicked()),ui->lineEdit,SLOT(setFocus())); 37 38 process->start("cmd.exe"); 39 } 40 41 MainWindow::~MainWindow() 42 { 43 if(process != Q_NULLPTR) 44 { 45 process->close(); 46 delete process; 47 process = Q_NULLPTR; 48 } 49 } 50 51 void MainWindow::write_cmd() 52 { 53 /* 获得命令,\r\n代替键盘的Enter,没有则不会执行 */ 54 QString cmd = ui->lineEdit->text() + "\r\n"; 55 ui->lineEdit->clear(); 56 ui->lineEdit->setFocus(); 57 58 QByteArray qbarr = cmd.toLatin1(); 59 char *ch = qbarr.data(); 60 qint64 len = cmd.length(); 61 process->write(ch, len); 62 63 /* 信息输出 */ 64 qDebug() << "Success:write_cmd:" << cmd << endl; 65 } 66 67 void MainWindow::read_data() 68 { 69 /* 接收数据 */ 70 QByteArray bytes = process->readAll(); 71 72 /* 显示 */ 73 QString msg = QString::fromLocal8Bit(bytes); 74 ui->textEdit->append(msg); 75 ui->textEdit->update(); 76 77 /* 信息输出 */ 78 qDebug() << "Success:read_data:" << msg << endl; 79 } 80 81 void MainWindow::finished_process() 82 { 83 /* 接收数据 */ 84 int flag = process->exitCode(); 85 86 /* 信息输出 */ 87 qDebug() << "Success:finishedProcess():" << flag << endl; 88 } 89 90 void MainWindow::error_process() 91 { 92 /* 接收数据 */ 93 int err_code = process->exitCode(); 94 QString err = process->errorString(); 95 96 /* 显示 */ 97 ui->textEdit->append(QString("error coed:%1").arg(err_code)); 98 ui->textEdit->append(err); 99 100 /* 信息输出 */ 101 qDebug() << "Success:error_process():" << err << endl; 102 }
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>MainWindow</class> 4 <widget class="QMainWindow" name="MainWindow"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>750</width> 10 <height>426</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralWidget"> 17 <widget class="QSplitter" name="splitter_3"> 18 <property name="geometry"> 19 <rect> 20 <x>31</x> 21 <y>10</y> 22 <width>681</width> 23 <height>341</height> 24 </rect> 25 </property> 26 <property name="orientation"> 27 <enum>Qt::Vertical</enum> 28 </property> 29 <widget class="QSplitter" name="splitter_2"> 30 <property name="orientation"> 31 <enum>Qt::Vertical</enum> 32 </property> 33 <widget class="QSplitter" name="splitter"> 34 <property name="orientation"> 35 <enum>Qt::Horizontal</enum> 36 </property> 37 <widget class="QPushButton" name="pushButton"> 38 <property name="text"> 39 <string>Clear</string> 40 </property> 41 </widget> 42 <widget class="QLineEdit" name="lineEdit"/> 43 <widget class="QPushButton" name="pushButton_Enter"> 44 <property name="text"> 45 <string>Enter</string> 46 </property> 47 </widget> 48 </widget> 49 <widget class="QTextEdit" name="textEdit"/> 50 </widget> 51 <widget class="QPushButton" name="pushButton_Clear"> 52 <property name="text"> 53 <string>Clear</string> 54 </property> 55 </widget> 56 </widget> 57 </widget> 58 <widget class="QMenuBar" name="menuBar"> 59 <property name="geometry"> 60 <rect> 61 <x>0</x> 62 <y>0</y> 63 <width>750</width> 64 <height>21</height> 65 </rect> 66 </property> 67 </widget> 68 <widget class="QToolBar" name="mainToolBar"> 69 <attribute name="toolBarArea"> 70 <enum>TopToolBarArea</enum> 71 </attribute> 72 <attribute name="toolBarBreak"> 73 <bool>false</bool> 74 </attribute> 75 </widget> 76 <widget class="QStatusBar" name="statusBar"/> 77 </widget> 78 <layoutdefault spacing="6" margin="11"/> 79 <resources/> 80 <connections/> 81 </ui>