日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Qt實現進程間通信_C 語言

作者:MechMaster ? 更新時間: 2022-10-16 編程語言

本文實例為大家分享了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

欄目分類
最近更新