Qt 进程通信QSharedMemory

学习了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_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private slots:
    void loadFromFileSlots();
    void loadFromMemorySlots();
private:
//    void detach();
private:
    Ui::Dialog *ui;
    QSharedMemory *shareMemory;
};

#endif // DIALOG_H

dialog.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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值