当前位置:网站首页>QT one process runs another
QT one process runs another
2022-04-23 12:15:00 【MechMaster】
Qt Interprocess communication
1. One process runs another process
- Qt Of QProcess Class is used to start and communicate with an external program .
- Use start() Function to run a process , after QProcess Get into Starting state ;
- When the program has run ,QProcess Will enter Running Status and send started() The signal ;
- When the process exits ,QProcess Return to NoRunning Status and launch finished() The signal .
2. Routine code

- 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);
// Add signal and slot Association
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()
{
// Start Notepad program , Because it's in Windows Under the system directory , The directory has been added to the system PATH In the environment variables , So you don't need to write a specific 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://yzsam.com/2022/04/202204231210423784.html
边栏推荐
- Pagoda panel command line help tutorial (including resetting password)
- CGC: contractual graph clustering for community detection and tracking
- Lesson 26 static member functions of classes
- 软件测试对于减少程序BUG有多大帮助?
- How to switch PHP version in Windows 2008 system
- Running error: unable to find or load the main class com xxx. Application
- 为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
- 第二十六课 类的静态成员函数
- In idea Solution to the problem of garbled code in Chinese display of properties file
- How much does software testing help reduce program bugs?
猜你喜欢
![[redis series] redis learning 13. Redis often asks simple interview questions](/img/4c/2440af6daa26a0ca6cb64f32bf138e.png)
[redis series] redis learning 13. Redis often asks simple interview questions

亿级流量架构,服务器如何扩容?写得太好了!

消息队列概述

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

编程辅助工具推荐:图片工具snipaste

初探 Lambda Powertools TypeScript

激活函数之sigmoid函数

Metalama简介4.使用Fabric操作项目或命名空间

worder字体网页字体对照表

The listing of saiweidian Technology Innovation Board broke: a decrease of 26% and the market value of the company was 4.4 billion
随机推荐
Sigmoid function of activation function
Chapter 4 is a tutorial on forced filling of in memory objects with IM enabled filling objects (IM 4.7)
1. Construction of electron development environment
AI video cloud vs narrowband HD, who is the darling of the video era
Qt一个进程运行另一个进程
Siri gave the most embarrassing social death moment of the year
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
【Redis 系列】redis 学习十三,Redis 常问简单面试题
A detailed explanation of head pose estimation [collection of good articles]
数据库如何填充IM表达式(IM 5.4)
同态加密技术学习
Running error: unable to find or load the main class com xxx. Application
NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
什么是网关
Lesson 26 static member functions of classes
After a circle, I sorted out this set of interview questions..
第二十四课 经典问题解析
第二十三课 临时对象
力扣-1137.第N个泰波那契数