当前位置:网站首页>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
边栏推荐
- kettle复制记录到结果和从结果获取记录使用
- The fourth chapter is the enable and disable columns of IM enabled fill objects (Part III of im-4.3)
- Recommended programming AIDS: picture tool snipaste
- Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
- 同态加密技术学习
- Idea setting copyright information
- Docker MySQL master-slave backup
- Symmetric encryption, certificate encryption
- 5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
- Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
猜你喜欢
如果你是一个Golang面试官,你会问哪些问题?
IDEA设置版权信息
2022 love analysis · panoramic report of industrial Internet manufacturers
亿级流量架构,服务器如何扩容?写得太好了!
运行报错:找不到或无法加载主类 com.xxx.Application
Worder font page font comparison table
为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
Recommended programming AIDS: picture tool snipaste
随机推荐
第四章 为IM 启用填充对象之强制填充In-Memory对象:教程(IM 4.7)
Im architecture: CPU architecture: SIMD vector processing (im-2.3)
After a circle, I sorted out this set of interview questions..
C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
Redis learning 5 - high concurrency distributed lock practice
亿级流量架构,服务器如何扩容?写得太好了!
异步时钟亚稳态 的解决方案——多bit信号
Idea code formatting plug-in save actions
5个免费音频素材网站,建议收藏
【Redis 系列】redis 学习十三,Redis 常问简单面试题
一文详解头部位姿估计【收藏好文】
Master slave replication configuration of MySQL
Step function of activation function
如果你是一个Golang面试官,你会问哪些问题?
使用连接组优化连接 (IM 6)
thinkphp 添加图片文字水印生成带二维码的推广海报
Use kettle to copy records to and get records from results
Here comes the detailed picture and text installation tutorial of H5 game
Windows2008系统如何切换PHP版本
MySQL 的主从复制配置