Qt 进程通信QSharedMemory
来源:互联网 发布:网页翻译软件 编辑:程序博客网 时间:2024/06/05 11:29
学习了Qt中进程通信的一种QSharedMemory
一下是测试代码,运行两个即可,一个load一个push
dialog.h#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QSharedMemory>#include <QFileDialog>#include <QIODevice>#include <QDir>#include <QPixmap>#include <QBuffer>#include <QDataStream>#include <stdio.h>#include <stdlib.h>#include <QDebug>#include <QImage>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()<<fileName; QImage image; if(!image.load(fileName)) { ui->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<<image; int size = buffer.size(); if(!shareMemory->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 <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); Dialog w; w.show(); return a.exec();}
0 0
- Qt 进程通信QSharedMemory
- Qt进程间通信 之使用QSharedMemory
- Qt共享内存实现进程间通信(QSharedMemory)
- QSharedMemory 共享内存 进程间通信
- qt QSharedMemory 用法
- QSharedMemory
- QSharedMemory
- qt 进程间通信
- QT进程间通信
- QT进程间通信
- qt 进程通信
- QT-进程间通信
- Qt进程间通信
- QT 共享内存 QSharedMemory 和 内存映射
- QT的内部进程通信
- QT的内部进程通信
- QT QCopChannel 进程间通信
- QT的内部进程通信
- hadoop二次排序==想通了
- IOS之sha加密、md5常规加密、md5二次加密详解及示例程序
- PHP定时执行的三种方式实现
- Oracle 的时区设置
- 浅谈算法和数据结构: 七 二叉查找树
- Qt 进程通信QSharedMemory
- Avoid Using The “system("pause")” !
- QT5入门之7 - 修改程序图标
- centos 6-7的epel源配置
- 最糟心的Linux密码问题!!!
- 11.Python标准库_多进程探索 (multiprocessing包)
- Java中ModelAndView是做什么的?
- Adobe Acrobat XI pro虚拟打印机的安装教程(附激活)
- IOS单例模式及单例模式的优缺点