Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Qt implement one process to run another

Shulou Source: shulou.com Published: 2022-05-31 19:16:26 10月05日 Update

This article "Qt how to achieve a process to run another process" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Qt how to achieve a process to run another process" article it.

1. One process runs another.

The QProcess class of Qt is used to start and communicate with an external program.

Use the start () function to run a process, after which QProcess enters the Starting state

When the program is already running, QProcess enters the Running state and sends a started () signal

When the process exits, the QProcess returns to the NoRunning state and emits the finished () signal.

two。 Routine code

Mainwindow.h

# ifndef MAINWINDOW_H#define MAINWINDOW_H#include # include namespace Ui {class MainWindow;} class MainWindow: public QMainWindow {Q_OBJECTpublic: explicit MainWindow (QWidget * parent = 0); ~ MainWindow (); private slots: void on_pushButton_clicked (); void showResult (); void showState (QProcess::ProcessState state); void showError (); void showFinished (int,QProcess::ExitStatus); private: Ui::MainWindow * ui; QProcess myProcess;}; # endif / / MAINWINDOW_H

Mainwindow.cpp

# include "mainwindow.h" # include "ui_mainwindow.h" # include # include MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); / / add connect (& myProcess,&QProcess::readyRead,this,&MainWindow::showResult) associated with signal and slot; connect (& myProcess,&QProcess::stateChanged,this,&MainWindow::showState) Connect (& myProcess,&QProcess::errorOccurred,this,&MainWindow::showError); connect (& myProcess,SIGNAL (finished (int,QProcess::ExitStatus)), this,SLOT (showFinished (int,QProcess::ExitStatus));} MainWindow::~MainWindow () {delete ui } void MainWindow::on_pushButton_clicked () {/ / starts the notepad program, since it is in the system directory of Windows, which has been added to the system PATH environment variable, there is no need to write the specific path / / myProcess.start ("notepad.exe"); QString program = "cmd.exe"; QStringList arguments; arguments

Tags: Process run content signal state program article directory knowledge article system code value function variable most that is more steps environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Docker Microsoft MariaDB