QT下进程之间通信——共享内存篇(1)

Linux中进程之间通信有,pipe,signal,消息队列,共享内存,信号量,socket.

Qt中提供的进程间通信有如下几种:

1.TCP/IP

跨平台的QNetwork提供了众多的类来实现网络编程.比如QNetworkAccessManger,Qftp等来使用指定的应用程序协议.

2.共享内存

在Linux中也有这个通信方式,通信方式是非常常用额,也比较简单.本文用共享内存来举例.

3.D-Bus

QtDBus模块是一个Unix库,可以使用D-Bus协议来实现进程间的通信

4.Qt通信协议(QCOP)

QCopChannel实现客户端程序使用有名管道来进行消息传输的协议.QCopChannel仅仅在在做嵌入式Linux上才能够使用.


新建一个Gui应用项目,

[html]  view plain  copy
  1. #include <QtGui/QApplication>  
  2. #include "dialog.h"  
  3. #include <QTextCodec>  
  4.   
  5.   
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QApplication a(argc, argv);  
  9.     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));  
  10.     Dialog w;  
  11.     w.show();  
  12.       
  13.     return a.exec();  
  14. }  

dialog.h

[html]  view plain  copy
  1. #ifndef DIALOG_H  
  2. #define DIALOG_H  
  3.   
  4. #include <QDialog>  
  5. #include <QSharedMemory>  
  6.   
  7. namespace Ui {  
  8. class Dialog;  
  9. }  
  10.   
  11. class Dialog : public QDialog  
  12. {  
  13.     Q_OBJECT  
  14.       
  15. public:  
  16.     explicit Dialog(QWidget *parent = 0);  
  17.     ~Dialog();  
  18.       
  19. private:  
  20.     Ui::Dialog *ui;  
  21.     void detach();  
  22.     QSharedMemory shareMemory;  
  23.   
  24. public slots:  
  25.     void loadFromFile();  
  26.     void loadFromMemory();  
  27.   
  28. private slots:  
  29.     void on_pushButton_clicked();  
  30.     void on_pushButton_2_clicked();  
  31. };  
  32.   
  33. #endif // DIALOG_H  

dialog.cpp

[html]  view plain  copy
  1. #include "dialog.h"  
  2. #include "ui_dialog.h"  
  3. #include <QFileDialog>  
  4. #include <QBuffer>  
  5. #include <QDebug>  
  6.   
  7. Dialog::Dialog(QWidget *parent) :  
  8.     QDialog(parent),  
  9.     ui(new Ui::Dialog)  
  10. {  
  11.     ui->setupUi(this);  
  12.     shareMemory.setKey("Linux_Google");  
  13. }  
  14.   
  15. Dialog::~Dialog()  
  16. {  
  17.     delete ui;  
  18. }  
  19.   
  20. void Dialog::loadFromFile()  
  21. {  
  22.     if(shareMemory.isAttached()) /* 判断该进程是否已经连接到共享内存段 */  
  23.         detach();  
  24.     ui->label->setText(tr("选择一个图片文件"));  
  25.     QString fileName = QFileDialog::getOpenFileName(0,QString(),QString(tr("Images(*.png *.jpg)")));  
  26.   
  27.     QImage image;  
  28.     if(!image.load(fileName))  
  29.     {  
  30.         ui->label->setText(tr("选择的文件不是图片文件,选择图片文件"));  
  31.         return;  
  32.     }  
  33.     ui->label->setPixmap(QPixmap::fromImage(image));  
  34.   
  35.     QBuffer buffer;  /* QBuffer用来暂存图片 */  
  36.     buffer.open(QBuffer::ReadWrite);  
  37.     QDataStream out(&buffer);   /* QDataStream从数据流冲去读取buffer数据 */  
  38.     out << image;  
  39.     int size = buffer.size();  
  40.     if(!shareMemory.create(size)){ /* 自动将该内存段连接到本进程上 */  
  41.         ui->label->setText(tr("无法创建共享内存段"));  
  42.         return;  
  43.     }  
  44.   
  45.     shareMemory.lock();  
  46.     char *to = (char *)shareMemory.data(); /* 强制转换一个地址 */  
  47.     const char *from=buffer.data().data();  
  48.     memcpy(to,from,qMin(shareMemory.size(),size));  
  49.     shareMemory.unlock();  
  50. }  
  51.   
  52. void Dialog::loadFromMemory()  
  53. {  
  54.     if(!shareMemory.attach()){  
  55.         ui->label->setText(tr("无法连接到共享内存段,请先添加一张图片"));  
  56.         return;  
  57.     }  
  58.   
  59.     QBuffer buffer;  
  60.     QDataStream in(&buffer);  
  61.     QImage image;  
  62.   
  63.     shareMemory.lock();  
  64.     buffer.setData((char*)shareMemory.constData(), shareMemory.size());  
  65.     buffer.open(QBuffer::ReadOnly);  
  66.     in >> image;  
  67.     shareMemory.unlock();  
  68.   
  69.     shareMemory.detach();  
  70.     ui->label->setPixmap(QPixmap::fromImage(image));  
  71. }  
  72.   
  73. void Dialog::detach()  
  74. {  
  75.     if (!shareMemory.detach())  
  76.         ui->label->setText(tr("无法从共享内存中分离"));  
  77. }  
  78.   
  79. void Dialog::on_pushButton_clicked()  
  80. {  
  81.     loadFromFile();  
  82. }  
  83.   
  84. void Dialog::on_pushButton_2_clicked()  
  85. {  
  86.     loadFromMemory();  
  87. }  

界面效果如下:



                                                     点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值