博客
关于我
Qt 进程通信QSharedMemory
阅读量:370 次
发布时间:2019-03-05

本文共 2352 字,大约阅读时间需要 7 分钟。

学习了Qt中进程通信的一种QSharedMemory

一下是测试代码,运行两个即可,一个load一个push

 
dialog.h#ifndef DIALOG_H#define DIALOG_H#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace Ui {class Dialog;}class Dialog : public QDialog{ Q_OBJECTpublic: explicit Dialog(QWidget *parent = 0); ~Dialog();private slots: void loadFromFileSlots(); void loadFromMemorySlots();private:// void detach();private: Ui::Dialog *ui; QSharedMemory *shareMemory;};#endif // DIALOG_Hdialog.cpp#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog){ ui->setupUi(this); this->shareMemory = new QSharedMemory("shareMemoryExample"); this->setWindowTitle("sharedMemory Example"); connect(ui->loadImageButton,SIGNAL(clicked()),SLOT(loadFromFileSlots())); connect(ui->pushMemoryButton,SIGNAL(clicked()),SLOT(loadFromMemorySlots()));}Dialog::~Dialog(){ delete ui;}void Dialog::loadFromFileSlots(){ if(shareMemory->isAttached()) { if (!shareMemory->detach()) ui->label->setText(tr("Unable to detach from shared memory.")); } ui->label->setText("Select a Image File!"); QString fileName = QFileDialog::getOpenFileName(0,"Open a Image file",QDir::currentPath(),("Image (*.png *.bmp *.jpg)")); qDebug()<
label->setText("Select file is not an Image, please select another."); return; } // ui->label->setPixmap(QPixmap::fromImage(image)); QBuffer buffer; if(buffer.open(QIODevice::ReadWrite)) { QDataStream in(&buffer); in<
create(size)) { ui->label->setText("Unable to creat shared memory!"); return; } shareMemory->lock(); char *to = (char *)shareMemory->data(); const char *from = buffer.data().data(); memcpy(to,from,qMin(shareMemory->size(),size)); shareMemory->unlock(); }}void Dialog::loadFromMemorySlots(){ if(!shareMemory->attach()) { ui->label->setText("Unable to attach to shared memory segment.\n"); return; } QBuffer buffer; QDataStream in(&buffer); QImage image; shareMemory->lock(); buffer.setData((char*)shareMemory->constData(),shareMemory->size()); buffer.open(QBuffer::ReadOnly); in>>image; shareMemory->unlock(); shareMemory->detach(); ui->label->setPixmap(QPixmap::fromImage(image));}main.cpp#include "dialog.h"#include
int main(int argc, char *argv[]){ QApplication a(argc, argv); Dialog w; w.show(); return a.exec();}

转载地址:http://bzcg.baihongyu.com/

你可能感兴趣的文章
mysql-5.7.18安装
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>