QT采用共享内存方式实现进程间通信

#include <QSharedMemory> //采用共享内存方式实现进程间通信

代码编译为release文件,所有依赖文件执行打包,生成可独立运行的程序,这样程序可以重复打开。

程序打开两次,第一个程序点第一个按钮,第二个程序点第二个按钮 

QT打包快捷生成依赖文件dll的方法_QT执行文件打包方法_txwtech的博客-CSDN博客1.添加D:\Qt\Qt5.14.1\5.14.1\mingw73_64\bin到环境变量path路径里面比如把qt生成的exe文件放在桌面test2文件里面,则运行如下:windeployqtC:\Users\txwtech\Desktop\test2,即可生成相应dll文件。手动添加dll,参考如下:https://blog.csdn.net/txwtech/article/details/113644271.........https://txwtech.blog.csdn.net/article/details/113646295

 创建项目文件基类选择QDialog

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QSharedMemory> //采用共享内存方式实现进程间通信

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();
public slots:
    void LoadFromFile();
    void LoadFromSharedMemory();

private slots:
    void on_pushButton_loadFromFile_clicked();

    void on_pushButton_loadFromSharedMemory_clicked();

private:
    Ui::Dialog *ui;
    void detach();
    QSharedMemory sharedMemo;
};
#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"
//采用共享内存方式实现进程间通信
#include <QFileDialog>
#include <QBuffer>
#include <QDebug>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    sharedMemo.setKey("QSharedMemoExample");//在使用共享内存以前,需要先为其指定一个key,系统用它来作为底层共享内存段的标识。
    //这个key可以是任意字符串
}

Dialog::~Dialog()
{
    delete ui;
}
void Dialog::LoadFromFile()
{
    if(sharedMemo.isAttached())//判断该进程是否连接到了共享内存段
    {
        detach();//进程与共享内存段进行分离
    }
    ui->label->setText(tr("请选择一个图片文件!"));
    QString fileName=QFileDialog::getOpenFileName(0,QString(),QString(),tr("Images(*.png *.jpg)"));
    QImage image;
    if(!image.load(fileName))
    {
        ui->label->setText(tr("选择的文件不是图片文件"));
        return;
    }
    ui->label->setPixmap(QPixmap::fromImage(image));//将图片加载到共享内存
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    QDataStream out(&buffer);
    out<<image; //图像放入数据流
    int size=buffer.size();
    if(!sharedMemo.create(size)) //创建共享内容
    {
        ui->label->setText(tr("创建共享内存段失败"));
        return;
    }
    sharedMemo.lock();//保证同一时刻只能有一个进程允许操作共享内存段
    char *to=(char *)sharedMemo.data();
    const char *from=buffer.data().data();
    memcpy(to,from,qMin(sharedMemo.size(),size));//数据段复制到共享内存段。txwtech
    sharedMemo.unlock();

}
void Dialog::LoadFromSharedMemory()
{
    if(!sharedMemo.attach())
    {
        ui->label->setText(tr("无法连接到共享内存段。\n请加载一张图片"));
        return;
    }
    QBuffer buffer2;
    QDataStream in2(&buffer2);
    QImage image2;
    sharedMemo.lock();
    buffer2.setData((char *) sharedMemo.constData(),sharedMemo.size());
    buffer2.open(QBuffer::ReadOnly);
    in2>>image2;
    sharedMemo.unlock();
    sharedMemo.detach();
    ui->label->setPixmap(QPixmap::fromImage(image2));

}
void Dialog::detach()
{
    if(!sharedMemo.detach())
    {
        ui->label->setText(tr("无法从共享内存中分离"));
    }
}


void Dialog::on_pushButton_loadFromFile_clicked()
{
    LoadFromFile();
}

void Dialog::on_pushButton_loadFromSharedMemory_clicked()
{
    LoadFromSharedMemory();
}

QT采用共享内存方式实现进程间通信.rar-QT文档类资源-CSDN下载QT采用共享内存方式实现进程间通信.rar博文链接:https://blog.csdn.net/更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/txwtech/86401323

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txwtech笛克特科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值