您当前的位置: 首页 >  qt

txwtech

暂无认证

  • 1浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

txwtech 发布时间:2022-08-15 22:54:09 ,浏览量:1

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

代码编译为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 
#include  //采用共享内存方式实现进程间通信

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 
#include 
#include 

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);
    outsetText(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

关注
打赏
1665060526
查看更多评论
立即登录/注册

微信扫码登录

0.0407s