The process used in QT needs to use the header file:include<qprocess>
First, let's look at the main functions that need to be used.
(1) Definition of the process:
// define a process parameter
(2) Initialization of the process:
// Initializing Process Objects New Qprocess ();
(3) Start process
// Start Calculator Mprocess->start ("calc"
(4) using process to read data
while // read all the data in the process
(5) How to use the character stitching Qstringlist
qstringlist list; // stitching strings, similar to Appen list<<"ech0"; List<<ui->comboBoxArg->currenttext (); Mprocess->start (Ui->comboboxcmd->currenttext (), list); // start the process, followed by the parameter
Head function: Process.h
1 #ifndef Process_h2 #defineProcess_h3 4#include <QMainWindow>5#include <QProcess>//header file for process in QT6 namespaceUi {7 classProcess;8 }9 Ten classProcess: PublicQmainwindow One { A Q_object - - Public: the ExplicitProcess (Qwidget *parent =0); -~Process (); -Qprocess *mprocess;//define a process parameter - + PrivateSlots: - voidRead_data (); + voidon_btn_cmd_clicked (); A at Private: -Ui::P rocess *UI; - }; - - #endif //Process_h
Source file: Process.cpp
1#include"process.h"2#include"Ui_process.h"3#include <QDebug>4#include <QStringList>5Process::P rocess (Qwidget *parent):6 Qmainwindow (parent),7UiNewUi::P rocess)8 {9UI->SETUPUI ( This);Ten //Initializing Process Objects OneMprocess =Newqprocess (); A - //listening to read data signals -Connect (mprocess,signal (readyread)), This, SLOT (Read_data ())); the - //add a Run parameter -Ui->comboboxarg->additem ("192.168.1.246"); -Ui->comboboxarg->additem ("./"); +Ui->comboboxarg->additem ("C + +"); - +Ui->comboboxcmd->additem ("ifconfig"); AUi->comboboxcmd->additem ("ls"); atUi->comboboxcmd->additem ("./test"); - - } - -process::~Process () - { in DeleteUI; - } to + - voidProcess::read_data () the { * while(Mprocess->canreadline ())//read all the data in the process $ {Panax NotoginsengQdebug () <<mprocess->readLine (); - } the + } A the voidprocess::on_btn_cmd_clicked () + { - qstringlist list; $ //stitching strings, similar to Appen $list<<"Ech0"; -List<<ui->comboboxarg->Currenttext (); - theMprocess->start (Ui->comboboxcmd->currenttext (), list);//start the process, followed by the parameter - Wuyi}
The use of processes in QT