当前位置:   article > 正文

Qt工作笔记-进程间的通信(通过QSharedMemory)_进程间通信 传图片

进程间通信 传图片

程序运行截图如下:

分享端分享图片:

接受端接受图片:

 

分享端的逻辑:

1.创建一个QSharedMemory,并设置一个key值;

2.查看这个内存是不是被使用,如果被使用就断开;

3.调用create进行创建;

4.使用memcpy把要写入的数据放入QSharedMemory中(内部一般会使用互斥锁技术,锁住资源)

 

读取端逻辑:

1.创建一个QSharedMemory,把key值设置为分享端相同的key值;

2.使用attach连接上这个QSharedMemory;

3.以读取字节的方式读取QSharedMemory中的数据(内部一般会使用互斥锁技术,锁住资源);

4.使用detach端口对这个QSharedMemory的连接。

 

源码如下:

接收端:

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. class QSharedMemory;
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit Widget(QWidget *parent = 0);
  13. ~Widget();
  14. public slots:
  15. void btnClicked();
  16. private:
  17. Ui::Widget *ui;
  18. QSharedMemory *m_sharedMemory;
  19. };
  20. #endif // WIDGET_H

main.cpp

  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QDebug>
  4. #include <QBuffer>
  5. #include <QDataStream>
  6. #include <QImage>
  7. #include <QPixmap>
  8. #include <QSharedMemory>
  9. Widget::Widget(QWidget *parent) :
  10. QWidget(parent),
  11. ui(new Ui::Widget)
  12. {
  13. ui->setupUi(this);
  14. connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
  15. m_sharedMemory=new QSharedMemory("169254");
  16. }
  17. Widget::~Widget()
  18. {
  19. delete ui;
  20. }
  21. void Widget::btnClicked(){
  22. if(!m_sharedMemory->attach()){
  23. qDebug()<<"SharedMemory attach failed!";
  24. return;
  25. }
  26. QBuffer buffer;
  27. QDataStream in(&buffer);
  28. QImage image;
  29. m_sharedMemory->lock();
  30. buffer.setData((char *)m_sharedMemory->constData(),m_sharedMemory->size());
  31. buffer.open(QBuffer::ReadOnly);
  32. in>>image;
  33. m_sharedMemory->unlock();
  34. m_sharedMemory->detach();
  35. ui->label->setPixmap(QPixmap::fromImage(image));
  36. }

 

分析端:

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. class QSharedMemory;
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit Widget(QWidget *parent = 0);
  13. ~Widget();
  14. public slots:
  15. void btnClicked();
  16. private:
  17. Ui::Widget *ui;
  18. QSharedMemory *m_sharedMemory;
  19. };
  20. #endif // WIDGET_H

main.cpp

  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QBuffer>
  4. #include <QFileDialog>
  5. #include <QDataStream>
  6. #include <QSharedMemory>
  7. #include <QDebug>
  8. #include <QPixmap>
  9. #include <QImage>
  10. Widget::Widget(QWidget *parent) :
  11. QWidget(parent),
  12. ui(new Ui::Widget)
  13. {
  14. ui->setupUi(this);
  15. connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
  16. m_sharedMemory=new QSharedMemory("169254");
  17. }
  18. Widget::~Widget()
  19. {
  20. delete ui;
  21. }
  22. void Widget::btnClicked()
  23. {
  24. if(m_sharedMemory->isAttached()){
  25. if(!m_sharedMemory->detach()){
  26. qDebug()<<"sharedMemory detach falied!";
  27. return;
  28. }
  29. }
  30. QString fileName=QFileDialog::getOpenFileName(this,"","","Images(*.*)");
  31. QImage image(fileName);
  32. ui->label->setPixmap(QPixmap::fromImage(image).scaled(128,128));
  33. QBuffer buffer;
  34. buffer.open(QBuffer::ReadWrite);
  35. QDataStream out(&buffer);
  36. out<<image;
  37. if(!m_sharedMemory->create(buffer.size())){
  38. qDebug()<<m_sharedMemory->errorString()<<" created failed!";
  39. return;
  40. }
  41. m_sharedMemory->lock();
  42. char *to=(char*)m_sharedMemory->data();
  43. const char *from=buffer.data().data();
  44. memcpy(to,from,qMin(m_sharedMemory->size(),(int)buffer.size()));
  45. m_sharedMemory->unlock();
  46. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/184737?site
推荐阅读
相关标签
  

闽ICP备14008679号