当前位置:网站首页>Qt进程间通信
Qt进程间通信
2022-04-23 12:10:00 【MechMaster】
Qt 进程间通信
1. 进程间通信的方法
- TCP/IP
Qt Network提供了众多的类来实现网络编程。 - 共享内存
QSharedMemory是跨平台的共享内存类,提供了访问操作系统共享内存的实现。它允许多个线程和进程安全地访问共享内存片段。此外,QSystemSemaphore可用于控制系统的共享资源的访问以及进程间通信。 - D-Bus
D-Bus模块是一个Unix库,可以使用D-Bus协议来实现进程间通信。它将Qt的信号和槽机制扩展到了IPC层面,允许一个进程发射的信号关联到另一个进程的槽上。 - QProcess
- 会话管理
在Linux/X11平台上,Qt提供了对会话管理的支持,回话允许时间传播到进程。例如,当关机时通知进程或程序,从而可以执行一些相关的操作。
2. 不同进程间共享内存示例代码
- dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QSharedMemory>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QSharedMemory sharedMemory;
void detach();
public slots:
void loadFromFile();
void loadFromMemory();
private slots:
void on_pushButtonLoadFromFile_clicked();
void on_pushButtonLoadFromSharedMemory_clicked();
};
#endif // DIALOG_H
- dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QBuffer>
#include <QDebug>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//在共享内存以前,需要先为其制定一个key,系统用它来作为底层共享内存段的标识。这个key可以是任意的字符串
sharedMemory.setKey("QSharedMemoryExample");
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::loadFromFile()
{
//判断该进程是否已经连接到共享内存段,如果是,就将该进程与共享内存段进行分离。
if(sharedMemory.isAttached())
detach();
ui->label->setText(tr("选择一个图片文件!"));
QString fileName = QFileDialog::getOpenFileName(0,QString(),QString(),tr("Images(*.png *.jpg)"));
QImage image;
if(!image.load(fileName))
{
ui->label->setText(tr("选择的文件不是图片,请选择图片文件"));
return;
}
ui->label->setPixmap((QPixmap::fromImage(image)));
//将图片加载到共享内存
QBuffer buffer;
//将图片暂存到buffer中
buffer.open(QBuffer::ReadWrite);
//获取图片数据的指针
QDataStream out(&buffer);
out<<image;
//获取图片的大小
int size = buffer.size();
//创建指定大小的共享内存段
if(!sharedMemory.create(size))
{
ui->label->setText(tr("无法创建共享内存段"));//
return;
}
//在共享内存段的操作时,需要先加锁
sharedMemory.lock();
char * to = (char*)sharedMemory.data();
const char * from = buffer.data().data();
memcpy(to,from,qMin(sharedMemory.size(),size));
//解锁
sharedMemory.unlock();
//如果将最后一个连接在共享内存段上的进程进行分离,那么系统会释放共享内存段。
}
void Dialog::loadFromMemory()
{
//将进程连接到共享内存段
if(!sharedMemory.attach())
{
ui->label->setText(tr("无法连接到共享内存段,\n"
"请先加载一张图片!"));
return;
}
QBuffer buffer;
QDataStream in(&buffer);
QImage image;
sharedMemory.lock();
//读取内存段中的数据
buffer.setData((char*)sharedMemory.constData(),sharedMemory.size());
buffer.open(QBuffer::ReadOnly);
in>>image;
sharedMemory.unlock();
sharedMemory.detach();
ui->label->setPixmap(QPixmap::fromImage(image));
}
void Dialog::detach()
{
if(!sharedMemory.detach())
{
ui->label->setText(tr("无法从共享内存中分离"));
}
}
void Dialog::on_pushButtonLoadFromFile_clicked()
{
loadFromFile();
}
void Dialog::on_pushButtonLoadFromSharedMemory_clicked()
{
loadFromMemory();
}
版权声明
本文为[MechMaster]所创,转载请带上原文链接,感谢
https://liuhui.blog.csdn.net/article/details/124336341
边栏推荐
- On using go language to create websocket service
- ImportError: libX11. so. 6: cannot open shared object file: No such file or directory
- Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5
- Recommended programming AIDS: picture tool snipaste
- 5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
- 欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
- 抓包整理————tcp 协议[八]
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- Im architecture: CPU architecture: SIMD vector processing (im-2.3)
- Lesson 24 analysis of classical problems
猜你喜欢
《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
Force buckle - 1137 Nth teponacci number
AI video cloud vs narrowband HD, who is the darling of the video era
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
Design and practice of the smallest short website system in the whole network
The maximum number of remote desktop servers has been exceeded
IDEA设置版权信息
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
Relu function of activation function
In idea Solution to the problem of garbled code in Chinese display of properties file
随机推荐
第二十三课 临时对象
Tclerror: no display name and no $display environment variable
什么是网关
Here comes the detailed picture and text installation tutorial of H5 game
How to switch PHP version in Windows 2008 system
Precautions for PCB
Fastjson 2 来了,性能继续提升,还能再战十年
On using go language to create websocket service
C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
宝塔面板命令行帮助教程(包含重置密码)
5个免费音频素材网站,建议收藏
软件测试基础DAY2-用例执行
VMware virtual machines export hard disk vmdk files using esxi
Array---
激活函数之sigmoid函数
C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
关于使用Go语言创建WebSocket服务浅谈