Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Qt implement one process to run another

2025-04-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report