博客
关于我
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重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>