Qt實現(xiàn)進程間通信
本文實例為大家分享了Qt實現(xiàn)進程間通信的具體代碼,供大家參考,具體內(nèi)容如下
1. 進程間通信的方法
1.TCP/IP
Qt Network提供了眾多的類來實現(xiàn)網(wǎng)絡(luò)編程。
2.共享內(nèi)存
QSharedMemory是跨平臺的共享內(nèi)存類,提供了訪問操作系統(tǒng)共享內(nèi)存的實現(xiàn)。它允許多個線程和進程安全地訪問共享內(nèi)存片段。此外,QSystemSemaphore可用于控制系統(tǒng)的共享資源的訪問以及進程間通信。
3.D-Bus
D-Bus模塊是一個Unix庫,可以使用D-Bus協(xié)議來實現(xiàn)進程間通信。它將Qt的信號和槽機制擴展到了IPC層面,允許一個進程發(fā)射的信號關(guān)聯(lián)到另一個進程的槽上。
4.QProcess
5.會話管理
在Linux/X11平臺上,Qt提供了對會話管理的支持,回話允許時間傳播到進程。例如,當(dāng)關(guān)機時通知進程或程序,從而可以執(zhí)行一些相關(guān)的操作。
2. 不同進程間共享內(nèi)存示例代碼

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_Hdialog.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);
? ? //在共享內(nèi)存以前,需要先為其制定一個key,系統(tǒng)用它來作為底層共享內(nèi)存段的標(biāo)識。這個key可以是任意的字符串
? ? sharedMemory.setKey("QSharedMemoryExample");
}
Dialog::~Dialog()
{
? ? delete ui;
}
void Dialog::loadFromFile()
{
? ? //判斷該進程是否已經(jīng)連接到共享內(nèi)存段,如果是,就將該進程與共享內(nèi)存段進行分離。
? ? 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)));
? ? //將圖片加載到共享內(nèi)存
? ? QBuffer buffer;
? ? //將圖片暫存到buffer中
? ? buffer.open(QBuffer::ReadWrite);
? ? //獲取圖片數(shù)據(jù)的指針
? ? QDataStream out(&buffer);
? ? out<<image;
? ? //獲取圖片的大小
? ? int size = buffer.size();
? ? //創(chuàng)建指定大小的共享內(nèi)存段
? ? if(!sharedMemory.create(size))
? ? {
? ? ? ? ui->label->setText(tr("無法創(chuàng)建共享內(nèi)存段"));//
? ? ? ? return;
? ? }
? ? //在共享內(nèi)存段的操作時,需要先加鎖
? ? sharedMemory.lock();
? ? char * to = (char*)sharedMemory.data();
? ? const char * from = buffer.data().data();
? ? memcpy(to,from,qMin(sharedMemory.size(),size));
? ? //解鎖
? ? sharedMemory.unlock();
? ? //如果將最后一個連接在共享內(nèi)存段上的進程進行分離,那么系統(tǒng)會釋放共享內(nèi)存段。
}
void Dialog::loadFromMemory()
{
? ? //將進程連接到共享內(nèi)存段
? ? if(!sharedMemory.attach())
? ? {
? ? ? ? ui->label->setText(tr("無法連接到共享內(nèi)存段,\n"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "請先加載一張圖片!"));
? ? ? ? return;
? ? }
? ? QBuffer buffer;
? ? QDataStream in(&buffer);
? ? QImage image;
? ? sharedMemory.lock();
? ? //讀取內(nèi)存段中的數(shù)據(jù)
? ? 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("無法從共享內(nèi)存中分離"));
? ? }
}
void Dialog::on_pushButtonLoadFromFile_clicked()
{
? ? loadFromFile();
}
void Dialog::on_pushButtonLoadFromSharedMemory_clicked()
{
? ? loadFromMemory();
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++深入探索內(nèi)聯(lián)函數(shù)inline與auto關(guān)鍵字的使用
本篇文章主要包括內(nèi)聯(lián)函數(shù)和auto關(guān)鍵字。其中,內(nèi)斂函數(shù)包括概念,特性等;auto關(guān)鍵字的使用規(guī)則,使用場景等,接下來讓我們深入了解2022-05-05
你知道如何自定義sort函數(shù)中的比較函數(shù)
這篇文章主要介紹了如何自定義sort函數(shù)中的比較函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

