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_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); ? ? //在共享內(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++/Php/Python 語言執(zhí)行shell命令的方法(推薦)
下面小編就為大家?guī)硪黄狢++/Php/Python 語言執(zhí)行shell命令的方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03C語言之實現(xiàn)控制臺光標(biāo)隨意移動的實例代碼
下面小編就為大家?guī)硪黄狢語言之實現(xiàn)控制臺光標(biāo)隨意移動的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07