当前位置:网站首页>Qt一个进程运行另一个进程
Qt一个进程运行另一个进程
2022-04-23 12:10:00 【MechMaster】
Qt 进程间通信
1. 一个进程运行另一个进程
- Qt 的 QProcess 类用来启动一个外部程序并与其进行通信。
- 使用 start() 函数运行一个进程,之后 QProcess 进入 Starting 状态;
- 当程序已经运行后,QProcess 就会进入 Running 状态并发送 started() 信号;
- 当进程退出后,QProcess 重新回到 NoRunning 状态并发射 finished() 信号。
2. 例程代码

- mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
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 <QDebug>
#include <QTextCodec>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//添加信号和槽的关联
connect(&myProcess,&QProcess::readyRead,this,&MainWindow::showResult);
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()
{
//启动记事本程序,由于它在Windows的系统目录下,该目录已经加在了系统PATH环境变量中,所以不需要写具体路径
//myProcess.start("notepad.exe");
QString program = "cmd.exe";
QStringList arguments;
arguments<<"/c dir&pause";
myProcess.start(program,arguments);
}
void MainWindow::showResult()
{
QTextCodec * codec = QTextCodec::codecForLocale();
qDebug()<<"showResult: "<<endl<<codec->toUnicode(myProcess.readAll());
}
void MainWindow::showState(QProcess::ProcessState state)
{
qDebug()<<"showState: ";
if(state == QProcess::NotRunning)
{
qDebug()<<"No Running";
}
else if(state == QProcess::Starting)
{
qDebug()<<"Starting";
}
else
{
qDebug()<<"Running";
}
}
void MainWindow::showError()
{
qDebug()<<"showError:"<<endl<<myProcess.errorString();
}
void MainWindow::showFinished(int exitcode, QProcess::ExitStatus exitStatue)
{
qDebug()<<"showFinished:"<<endl<<exitcode<<exitStatue;
}
版权声明
本文为[MechMaster]所创,转载请带上原文链接,感谢
https://liuhui.blog.csdn.net/article/details/124300275
边栏推荐
- Idea database navigator plug-in
- Force buckle - 1137 Nth teponacci number
- 为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
- Tensorflow common functions
- 1. Construction of electron development environment
- MySQL 的主从复制配置
- VMware virtual machines export hard disk vmdk files using esxi
- After a circle, I sorted out this set of interview questions..
- Precautions for PCB
- Chapter 5 optimizing queries using in memory expressions (IM 5.1)
猜你喜欢

电脑系统卡如何解决?

PSCP basic usage

《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿

NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol

智能多线弹性云增加独立的IP地址,如何实现多线功能?

运行报错:找不到或无法加载主类 com.xxx.Application

Win10 splash screen after startup

5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型

In idea Solution to the problem of garbled code in Chinese display of properties file
随机推荐
What is a gateway
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
Tensorflow common functions
NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
IM 体系结构:CPU架构:SIMD向量处理(IM-2.3)
Idea database navigator plug-in
Force buckle - 1137 Nth teponacci number
用户接口和IM表达式(IM 5.6)
Tan Xiang, CEO of Kechuang · Pera software: the essence of zero trust is digital security. To B should also deeply study the user's mind
MySQL 的主从复制配置
Lesson 25 static member variables of classes
Lesson 23 temporary objects
IFLYTEK's revenue in 2021 was 18.3 billion yuan: a year-on-year increase of 41% and a net profit of 1.556 billion yuan
Castle.DynamicProxy实现事务单元控制
Database design of forum system
激活函数之阶跃函数
On lambda powertools typescript
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
第五章 使用In-Memory表达式优化查询(IM 5.1)
使用连接组优化连接 (IM 6)