赞
踩
程序运行截图如下:
分享端分享图片:
接受端接受图片:
分享端的逻辑:
1.创建一个QSharedMemory,并设置一个key值;
2.查看这个内存是不是被使用,如果被使用就断开;
3.调用create进行创建;
4.使用memcpy把要写入的数据放入QSharedMemory中(内部一般会使用互斥锁技术,锁住资源)
读取端逻辑:
1.创建一个QSharedMemory,把key值设置为分享端相同的key值;
2.使用attach连接上这个QSharedMemory;
3.以读取字节的方式读取QSharedMemory中的数据(内部一般会使用互斥锁技术,锁住资源);
4.使用detach端口对这个QSharedMemory的连接。
源码如下:
接收端:
widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
-
- class QSharedMemory;
-
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
-
- public slots:
- void btnClicked();
-
- private:
- Ui::Widget *ui;
-
- QSharedMemory *m_sharedMemory;
- };
-
- #endif // WIDGET_H
main.cpp
- #include "widget.h"
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
-
- return a.exec();
- }
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
-
- #include <QDebug>
- #include <QBuffer>
- #include <QDataStream>
- #include <QImage>
- #include <QPixmap>
- #include <QSharedMemory>
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
-
- m_sharedMemory=new QSharedMemory("169254");
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::btnClicked(){
- if(!m_sharedMemory->attach()){
- qDebug()<<"SharedMemory attach failed!";
- return;
- }
- QBuffer buffer;
- QDataStream in(&buffer);
- QImage image;
-
- m_sharedMemory->lock();
- buffer.setData((char *)m_sharedMemory->constData(),m_sharedMemory->size());
- buffer.open(QBuffer::ReadOnly);
- in>>image;
- m_sharedMemory->unlock();
- m_sharedMemory->detach();
-
- ui->label->setPixmap(QPixmap::fromImage(image));
-
- }
分析端:
widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
-
- class QSharedMemory;
-
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
-
- public slots:
- void btnClicked();
-
- private:
- Ui::Widget *ui;
- QSharedMemory *m_sharedMemory;
- };
-
- #endif // WIDGET_H
main.cpp
- #include "widget.h"
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
-
- return a.exec();
- }
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
-
- #include <QBuffer>
- #include <QFileDialog>
- #include <QDataStream>
- #include <QSharedMemory>
- #include <QDebug>
- #include <QPixmap>
- #include <QImage>
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
-
- m_sharedMemory=new QSharedMemory("169254");
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::btnClicked()
- {
- if(m_sharedMemory->isAttached()){
- if(!m_sharedMemory->detach()){
- qDebug()<<"sharedMemory detach falied!";
- return;
- }
- }
-
- QString fileName=QFileDialog::getOpenFileName(this,"","","Images(*.*)");
- QImage image(fileName);
- ui->label->setPixmap(QPixmap::fromImage(image).scaled(128,128));
-
- QBuffer buffer;
- buffer.open(QBuffer::ReadWrite);
- QDataStream out(&buffer);
- out<<image;
-
- if(!m_sharedMemory->create(buffer.size())){
- qDebug()<<m_sharedMemory->errorString()<<" created failed!";
- return;
- }
-
- m_sharedMemory->lock();
- char *to=(char*)m_sharedMemory->data();
- const char *from=buffer.data().data();
- memcpy(to,from,qMin(m_sharedMemory->size(),(int)buffer.size()));
- m_sharedMemory->unlock();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。