網站首頁 編程語言 正文
本文實例為大家分享了Qt實現進程間通信的具體代碼,供大家參考,具體內容如下
1. 進程間通信的方法
1.TCP/IP
Qt Network提供了眾多的類來實現網絡編程。
2.共享內存
QSharedMemory是跨平臺的共享內存類,提供了訪問操作系統共享內存的實現。它允許多個線程和進程安全地訪問共享內存片段。此外,QSystemSemaphore可用于控制系統的共享資源的訪問以及進程間通信。
3.D-Bus
D-Bus模塊是一個Unix庫,可以使用D-Bus協議來實現進程間通信。它將Qt的信號和槽機制擴展到了IPC層面,允許一個進程發射的信號關聯到另一個進程的槽上。
4.QProcess
5.會話管理
在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();
}
原文鏈接:https://blog.csdn.net/weixin_38566632/article/details/124336341
相關推薦
- 2021-12-16 方案缺陷-HAProxy + Sentinel +redis
- 2022-04-27 Django與DRF結合的全局異常處理方案詳解_python
- 2022-06-19 WPF實現流光動畫特效_實用技巧
- 2022-07-28 Python?datacompy?找出兩個DataFrames不同的地方_python
- 2022-10-17 shell腳本的流程控制語句的實現_linux shell
- 2023-02-27 python實現定時任務的八種方式總結_python
- 2022-10-27 在Rust?web服務中使用Redis的方法_Redis
- 2022-02-18 微信小程序----------父組件調用子組件的方法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支