qt中的进程使用需要用到头文件:include<QProcess>
首先来看看需要用到的主要的函数
(1)进程的定义:
QProcess *mprocess; //定义一个进程参数
(2)进程的初始化:
//初始化进程对象 mprocess = new QProcess();
(3)启动进程
//启动计算器 mprocess->start("calc");
(4)使用进程读取数据
while(mprocess->canReadLine()) //全部读出进程中的数据
(5)字符拼接QStringList的使用方法
QStringList list; //拼接字符串,跟appen相似 list<<"ech0"; list<comboBoxArg->currentText(); mprocess->start(ui->comboBoxCmd->currentText(),list);//启动进程,后面跟的是参数
头函数:process.h
1 #ifndef PROCESS_H 2 #define PROCESS_H 3 4 #include5 #include //qt中进程的头文件 6 namespace Ui { 7 class Process; 8 } 9 10 class Process : public QMainWindow11 {12 Q_OBJECT13 14 public:15 explicit Process(QWidget *parent = 0);16 ~Process();17 QProcess *mprocess; //定义一个进程参数18 19 private slots:20 void read_data();21 void on_btn_cmd_clicked();22 23 private:24 Ui::Process *ui;25 };26 27 #endif // PROCESS_H
源文件:process.cpp
1 #include "process.h" 2 #include "ui_process.h" 3 #include4 #include 5 Process::Process(QWidget *parent) : 6 QMainWindow(parent), 7 ui(new Ui::Process) 8 { 9 ui->setupUi(this);10 //初始化进程对象11 mprocess = new QProcess();12 13 //监听读数据信号14 connect(mprocess,SIGNAL(readyRead()),this,SLOT(read_data()));15 16 //添加运行参数17 ui->comboBoxArg->addItem("192.168.1.246");18 ui->comboBoxArg->addItem("./");19 ui->comboBoxArg->addItem("c++");20 21 ui->comboBoxCmd->addItem("ifconfig");22 ui->comboBoxCmd->addItem("ls");23 ui->comboBoxCmd->addItem("./test");24 25 }26 27 Process::~Process()28 {29 delete ui;30 }31 32 33 void Process::read_data()34 {35 while(mprocess->canReadLine()) //全部读出进程中的数据36 {37 qDebug()< readLine();38 }39 40 }41 42 void Process::on_btn_cmd_clicked()43 {44 QStringList list;45 //拼接字符串,跟appen相似46 list<<"ech0";47 list< comboBoxArg->currentText();48 49 mprocess->start(ui->comboBoxCmd->currentText(),list);//启动进程,后面跟的是参数50 51 }