Qt實(shí)現(xiàn)進(jìn)程間通信
本文實(shí)例為大家分享了Qt實(shí)現(xiàn)進(jìn)程間通信的具體代碼,供大家參考,具體內(nèi)容如下
1. 進(jìn)程間通信的方法
1.TCP/IP
Qt Network提供了眾多的類來實(shí)現(xiàn)網(wǎng)絡(luò)編程。
2.共享內(nèi)存
QSharedMemory是跨平臺(tái)的共享內(nèi)存類,提供了訪問操作系統(tǒng)共享內(nèi)存的實(shí)現(xiàn)。它允許多個(gè)線程和進(jìn)程安全地訪問共享內(nèi)存片段。此外,QSystemSemaphore可用于控制系統(tǒng)的共享資源的訪問以及進(jìn)程間通信。
3.D-Bus
D-Bus模塊是一個(gè)Unix庫,可以使用D-Bus協(xié)議來實(shí)現(xiàn)進(jìn)程間通信。它將Qt的信號(hào)和槽機(jī)制擴(kuò)展到了IPC層面,允許一個(gè)進(jìn)程發(fā)射的信號(hào)關(guān)聯(lián)到另一個(gè)進(jìn)程的槽上。
4.QProcess
5.會(huì)話管理
在Linux/X11平臺(tái)上,Qt提供了對(duì)會(huì)話管理的支持,回話允許時(shí)間傳播到進(jìn)程。例如,當(dāng)關(guān)機(jī)時(shí)通知進(jìn)程或程序,從而可以執(zhí)行一些相關(guān)的操作。
2. 不同進(jìn)程間共享內(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)存以前,需要先為其制定一個(gè)key,系統(tǒng)用它來作為底層共享內(nèi)存段的標(biāo)識(shí)。這個(gè)key可以是任意的字符串
? ? sharedMemory.setKey("QSharedMemoryExample");
}
Dialog::~Dialog()
{
? ? delete ui;
}
void Dialog::loadFromFile()
{
? ? //判斷該進(jìn)程是否已經(jīng)連接到共享內(nèi)存段,如果是,就將該進(jìn)程與共享內(nèi)存段進(jìn)行分離。
? ? if(sharedMemory.isAttached())
? ? ? ? detach();
? ? ui->label->setText(tr("選擇一個(gè)圖片文件!"));
? ? QString fileName = QFileDialog::getOpenFileName(0,QString(),QString(),tr("Images(*.png *.jpg)"));
? ? QImage image;
? ? if(!image.load(fileName))
? ? {
? ? ? ? ui->label->setText(tr("選擇的文件不是圖片,請(qǐng)選擇圖片文件"));
? ? ? ? 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)存段的操作時(shí),需要先加鎖
? ? sharedMemory.lock();
? ? char * to = (char*)sharedMemory.data();
? ? const char * from = buffer.data().data();
? ? memcpy(to,from,qMin(sharedMemory.size(),size));
? ? //解鎖
? ? sharedMemory.unlock();
? ? //如果將最后一個(gè)連接在共享內(nèi)存段上的進(jìn)程進(jìn)行分離,那么系統(tǒng)會(huì)釋放共享內(nèi)存段。
}
void Dialog::loadFromMemory()
{
? ? //將進(jìn)程連接到共享內(nèi)存段
? ? if(!sharedMemory.attach())
? ? {
? ? ? ? ui->label->setText(tr("無法連接到共享內(nèi)存段,\n"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "請(qǐng)先加載一張圖片!"));
? ? ? ? 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();
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Qt實(shí)現(xiàn)簡(jiǎn)單的TCP通信
- QT編寫tcp通信工具(Client篇)
- QT編寫tcp通信工具(Server端)
- QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn)詳解
- Qt?TCP網(wǎng)絡(luò)通信學(xué)習(xí)
- Qt網(wǎng)絡(luò)編程實(shí)現(xiàn)TCP通信
- Qt實(shí)現(xiàn)簡(jiǎn)單UDP通信
- QT5實(shí)現(xiàn)簡(jiǎn)單的TCP通信的實(shí)現(xiàn)
- 基于QT的TCP通信服務(wù)的實(shí)現(xiàn)
- QT5實(shí)現(xiàn)UDP通信的示例代碼
- QT串口通信的實(shí)現(xiàn)方法
相關(guān)文章
C++/Php/Python 語言執(zhí)行shell命令的方法(推薦)
下面小編就為大家?guī)硪黄狢++/Php/Python 語言執(zhí)行shell命令的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
C語言實(shí)現(xiàn)簡(jiǎn)單班級(jí)成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡(jiǎn)單班級(jí)成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C語言之實(shí)現(xiàn)控制臺(tái)光標(biāo)隨意移動(dòng)的實(shí)例代碼
下面小編就為大家?guī)硪黄狢語言之實(shí)現(xiàn)控制臺(tái)光標(biāo)隨意移動(dòng)的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07

