由于工作需求,要使用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
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QProcess> #include <handleapi.h> #include <QShortcut> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; QProcess *process; QShortcut *key; public slots: void write_cmd(); void read_data(); void finished_process(); void error_process(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QShortcut> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { setWindowTitle("cmd text"); ui->setupUi(this); ui->lineEdit->setText(tr("ping zhangmengmm.top")); ui->textEdit->setLineWrapMode(QTextEdit::NoWrap); process = new QProcess(this); process->setProcessChannelMode(QProcess::MergedChannels); ui->pushButton_Enter->setFocus(); ui->pushButton_Enter->setShortcut(QKeySequence::InsertParagraphSeparator); ui->pushButton_Enter->setShortcut(Qt::Key_Enter); ui->pushButton_Enter->setShortcut(Qt::Key_Return); connect(ui->pushButton,SIGNAL(clicked()),ui->lineEdit,SLOT(clear())); //清除命令行 connect(ui->pushButton_Enter,SIGNAL(clicked()),this,SLOT(write_cmd())); //回车按钮 connect(ui->pushButton_Clear,SIGNAL(clicked()),ui->textEdit,SLOT(clear())); //清除文本框按钮 connect(process,SIGNAL(readyRead()),this,SLOT(read_data())); //读命令行数据 connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(read_data())); //读命令行标准数据(兼容) connect(process,SIGNAL(errorOccurred(QProcess::ProcessError)),this,SLOT(error_process())); //命令行错误处理 connect(process,SIGNAL(finished(int)),this,SLOT(finished_process())); //命令行结束处理 connect(ui->pushButton,SIGNAL(clicked()),ui->lineEdit,SLOT(setFocus())); connect(ui->pushButton_Enter,SIGNAL(clicked()),ui->lineEdit,SLOT(setFocus())); connect(ui->pushButton_Clear,SIGNAL(clicked()),ui->lineEdit,SLOT(setFocus())); process->start("cmd.exe"); } MainWindow::~MainWindow() { if(process != Q_NULLPTR) { process->close(); delete process; process = Q_NULLPTR; } } void MainWindow::write_cmd() { /* 获得命令,\r\n代替键盘的Enter,没有则不会执行 */ QString cmd = ui->lineEdit->text() + "\r\n"; ui->lineEdit->clear(); ui->lineEdit->setFocus(); QByteArray qbarr = cmd.toLatin1(); char *ch = qbarr.data(); qint64 len = cmd.length(); process->write(ch, len); /* 信息输出 */ qDebug() << "Success:write_cmd:" << cmd << endl; } void MainWindow::read_data() { /* 接收数据 */ QByteArray bytes = process->readAll(); /* 显示 */ QString msg = QString::fromLocal8Bit(bytes); ui->textEdit->append(msg); ui->textEdit->update(); /* 信息输出 */ qDebug() << "Success:read_data:" << msg << endl; } void MainWindow::finished_process() { /* 接收数据 */ int flag = process->exitCode(); /* 信息输出 */ qDebug() << "Success:finishedProcess():" << flag << endl; } void MainWindow::error_process() { /* 接收数据 */ int err_code = process->exitCode(); QString err = process->errorString(); /* 显示 */ ui->textEdit->append(QString("error coed:%1").arg(err_code)); ui->textEdit->append(err); /* 信息输出 */ qDebug() << "Success:error_process():" << err << endl; }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>750</width> <height>426</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QSplitter" name="splitter_3"> <property name="geometry"> <rect> <x>31</x> <y>10</y> <width>681</width> <height>341</height> </rect> </property> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <widget class="QSplitter" name="splitter_2"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <widget class="QSplitter" name="splitter"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>Clear</string> </property> </widget> <widget class="QLineEdit" name="lineEdit"/> <widget class="QPushButton" name="pushButton_Enter"> <property name="text"> <string>Enter</string> </property> </widget> </widget> <widget class="QTextEdit" name="textEdit"/> </widget> <widget class="QPushButton" name="pushButton_Clear"> <property name="text"> <string>Clear</string> </property> </widget> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>750</width> <height>21</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>