博客
关于我
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 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>