当前位置:网站首页>QT interprocess communication
QT interprocess communication
2022-04-23 12:15:00 【MechMaster】
Qt Interprocess communication
1. Method of interprocess communication
- TCP/IP
Qt Network It provides many classes to realize network programming . - Shared memory
QSharedMemory Is a cross platform shared memory class , Provides the implementation of accessing the shared memory of the operating system . It allows multiple threads and processes to safely access shared memory fragments . Besides ,QSystemSemaphore It can be used to control the access of shared resources and inter process communication of the system . - D-Bus
D-Bus The module is a Unix library , have access to D-Bus Protocol to realize interprocess communication . It will Qt The signal and slot mechanism is extended to IPC level , Allows a signal emitted by one process to be associated with another process's slot . - QProcess
- session management
stay Linux/X11 On the platform ,Qt Provides support for session management , The reply allows time to propagate to the process . for example , Notify processes or programs when shutdown , So you can perform some related operations .
2. Sample code of shared memory between different processes

- 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);
// Before sharing memory , You need to develop a key, The system uses it as the identification of the underlying shared memory segment . This key Can be any string
sharedMemory.setKey("QSharedMemoryExample");
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::loadFromFile()
{
// Determine whether the process is connected to the shared memory segment , If it is , Separate the process from the shared memory segment .
if(sharedMemory.isAttached())
detach();
ui->label->setText(tr(" Select a picture file !"));
QString fileName = QFileDialog::getOpenFileName(0,QString(),QString(),tr("Images(*.png *.jpg)"));
QImage image;
if(!image.load(fileName))
{
ui->label->setText(tr(" The selected file is not a picture , Please select a picture file "));
return;
}
ui->label->setPixmap((QPixmap::fromImage(image)));
// Load pictures into shared memory
QBuffer buffer;
// Save pictures to buffer in
buffer.open(QBuffer::ReadWrite);
// Get the pointer of picture data
QDataStream out(&buffer);
out<<image;
// Get the size of the picture
int size = buffer.size();
// Create a shared memory segment of the specified size
if(!sharedMemory.create(size))
{
ui->label->setText(tr(" Unable to create shared memory segment "));//
return;
}
// During the operation of shared memory segment , It needs to be locked first
sharedMemory.lock();
char * to = (char*)sharedMemory.data();
const char * from = buffer.data().data();
memcpy(to,from,qMin(sharedMemory.size(),size));
// Unlock
sharedMemory.unlock();
// If you separate the last process connected to the shared memory segment , Then the system will release the shared memory segment .
}
void Dialog::loadFromMemory()
{
// Connect the process to the shared memory segment
if(!sharedMemory.attach())
{
ui->label->setText(tr(" Unable to connect to shared memory segment ,\n"
" Please load a picture first !"));
return;
}
QBuffer buffer;
QDataStream in(&buffer);
QImage image;
sharedMemory.lock();
// Read the data in the memory segment
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(" Cannot detach from shared memory "));
}
}
void Dialog::on_pushButtonLoadFromFile_clicked()
{
loadFromFile();
}
void Dialog::on_pushButtonLoadFromSharedMemory_clicked()
{
loadFromMemory();
}
版权声明
本文为[MechMaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231210423733.html
边栏推荐
- Chapter 5 optimizing queries using in memory expressions (IM 5.1)
- Outsourcing for five years, abandoned
- Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
- IDEA 代码格式化插件Save Actions
- Idea setting copyright information
- C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
- 异步时钟亚稳态 的解决方案——多bit信号
- The database navigator uses the default MySQL connection prompt: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä’ is unrecognized or repres
- AI video cloud vs narrowband HD, who is the darling of the video era
- [redis series] redis learning 13. Redis often asks simple interview questions
猜你喜欢

科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智

Next. JS static data generation and server-side rendering

SQL exercise (I)

为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?

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

CGC: contractual graph clustering for community detection and tracking

In idea Solution to the problem of garbled code in Chinese display of properties file

Tips for installing MySQL service in windows11: Install / Remove of the Service denied

Qt双缓冲绘图

激活函数之阶跃函数
随机推荐
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
Purpose of IM expression (IM 5.2)
在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决
Castle. Dynamic proxy implements transaction unit control
How the database fills in IM expressions (IM 5.4)
How do programmers finalize nucleic acid statistics with 130 lines of code
第二十六课 类的静态成员函数
力扣-1137.第N个泰波那契数
On lambda powertools typescript
Relu function of activation function
为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
Idea code formatting plug-in save actions
第二十四课 经典问题解析
Qt一个进程运行另一个进程
Chapter 4 specifies the attribute of the inmemory column on the no inmemory table for im enabled filling objects: examples (Part IV of im-4.4)
What is a gateway
Force buckle - 1137 Nth teponacci number
第四章 为IM 启用填充对象之为IM列存储启用ADO(IM 4.8)