程序运行截图如下:
分享端分享图片:
接受端接受图片:
分享端的逻辑:
1.创建一个QSharedMemory,并设置一个key值;
2.查看这个内存是不是被使用,如果被使用就断开;
3.调用create进行创建;
4.使用memcpy把要写入的数据放入QSharedMemory中(内部一般会使用互斥锁技术,锁住资源)
读取端逻辑:
1.创建一个QSharedMemory,把key值设置为分享端相同的key值;
2.使用attach连接上这个QSharedMemory;
3.以读取字节的方式读取QSharedMemory中的数据(内部一般会使用互斥锁技术,锁住资源);
4.使用detach端口对这个QSharedMemory的连接。
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
源码如下:
接收端:
widget.h
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5
6 class QSharedMemory;
7
8 namespace Ui {
9 class Widget;
10 }
11
12 class Widget : public QWidget
13 {
14 Q_OBJECT
15
16 public:
17 explicit Widget(QWidget *parent = 0);
18 ~Widget();
19
20 public slots:
21 void btnClicked();
22
23 private:
24 Ui::Widget *ui;
25
26 QSharedMemory *m_sharedMemory;
27 };
28
29 #endif // WIDGET_H
main.cpp
1 #include "widget.h"
2 #include <QApplication>
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 Widget w;
8 w.show();
9
10 return a.exec();
11 }
widget.cpp
1 #include "widget.h"
2 #include "ui_widget.h"
3
4 #include <QDebug>
5 #include <QBuffer>
6 #include <QDataStream>
7 #include <QImage>
8 #include <QPixmap>
9 #include <QSharedMemory>
10
11 Widget::Widget(QWidget *parent) :
12 QWidget(parent),
13 ui(new Ui::Widget)
14 {
15 ui->setupUi(this);
16 connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
17
18 m_sharedMemory=new QSharedMemory("169254");
19 }
20
21 Widget::~Widget()
22 {
23 delete ui;
24 }
25
26 void Widget::btnClicked(){
27 if(!m_sharedMemory->attach()){
28 qDebug()<<"SharedMemory attach failed!";
29 return;
30 }
31 QBuffer buffer;
32 QDataStream in(&buffer);
33 QImage image;
34
35 m_sharedMemory->lock();
36 buffer.setData((char *)m_sharedMemory->constData(),m_sharedMemory->size());
37 buffer.open(QBuffer::ReadOnly);
38 in>>image;
39 m_sharedMemory->unlock();
40 m_sharedMemory->detach();
41
42 ui->label->setPixmap(QPixmap::fromImage(image));
43
44 }
分析端:
widget.h
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5
6 class QSharedMemory;
7
8 namespace Ui {
9 class Widget;
10 }
11
12 class Widget : public QWidget
13 {
14 Q_OBJECT
15
16 public:
17 explicit Widget(QWidget *parent = 0);
18 ~Widget();
19
20 public slots:
21 void btnClicked();
22
23 private:
24 Ui::Widget *ui;
25 QSharedMemory *m_sharedMemory;
26 };
27
28 #endif // WIDGET_H
main.cpp
1 #include "widget.h"
2 #include <QApplication>
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 Widget w;
8 w.show();
9
10 return a.exec();
11 }
widget.cpp
1 #include "widget.h"
2 #include "ui_widget.h"
3
4 #include <QBuffer>
5 #include <QFileDialog>
6 #include <QDataStream>
7 #include <QSharedMemory>
8 #include <QDebug>
9 #include <QPixmap>
10 #include <QImage>
11
12 Widget::Widget(QWidget *parent) :
13 QWidget(parent),
14 ui(new Ui::Widget)
15 {
16 ui->setupUi(this);
17 connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
18
19 m_sharedMemory=new QSharedMemory("169254");
20 }
21
22 Widget::~Widget()
23 {
24 delete ui;
25 }
26
27 void Widget::btnClicked()
28 {
29 if(m_sharedMemory->isAttached()){
30 if(!m_sharedMemory->detach()){
31 qDebug()<<"sharedMemory detach falied!";
32 return;
33 }
34 }
35
36 QString fileName=QFileDialog::getOpenFileName(this,"","","Images(*.*)");
37 QImage image(fileName);
38 ui->label->setPixmap(QPixmap::fromImage(image).scaled(128,128));
39
40 QBuffer buffer;
41 buffer.open(QBuffer::ReadWrite);
42 QDataStream out(&buffer);
43 out<<image;
44
45 if(!m_sharedMemory->create(buffer.size())){
46 qDebug()<<m_sharedMemory->errorString()<<" created failed!";
47 return;
48 }
49
50 m_sharedMemory->lock();
51 char *to=(char*)m_sharedMemory->data();
52 const char *from=buffer.data().data();
53 memcpy(to,from,qMin(m_sharedMemory->size(),(int)buffer.size()));
54 m_sharedMemory->unlock();
55 }
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓