Qt实现IPC进程间通信-共享内存方式

Qt实现IPC进程间通信-共享内存方式

  • 1 Qt实现IPC进程间通信-共享内存方式

1 Qt实现IPC进程间通信-共享内存方式

原文链接:https://blog.csdn.net/weixin_40355471/article/details/113176745

QT封装了QSharedMemory类实现共享内存,可以共享文本、图片等各种数据格式,支持windows和linux跨平台使用;
示例代码新建两个工程,分别启动两个应用程序,实现文本数据的共享。

工程A
widget_pa.h

#ifndef WIDGET_PA_H
#define WIDGET_PA_H

#include 
#include 
namespace Ui {
class Widget_PA;
}
class Widget_PA : public QWidget
{
    Q_OBJECT
public:
    explicit Widget_PA(QWidget *parent = 0);
    ~Widget_PA();
    void sendStrToMemory(QString str);
private slots:
    void on_pushButton_clicked();
private:
    Ui::Widget_PA *ui;
    QSharedMemory m_sharedMemory;
};

#endif // WIDGET_PA_H

widget_pa.cpp

#include "widget_pa.h"
#include "ui_widget_pa.h"

#include 
#include 

Widget_PA::Widget_PA(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_PA)
{
    ui->setupUi(this);
}

Widget_PA::~Widget_PA()
{
    delete ui;
}

void Widget_PA::sendStrToMemory(QString str)
{
    //1、设置共享内存的标志名
    m_sharedMemory.setKey("processAstr");
    if(m_sharedMemory.isAttached())
    {
        //2、将该进程与共享内存分离
        if(!m_sharedMemory.detach())
        {
            qDebug() << "Unable to detach from shared memory.";
        }
    }
    // 将数据加载到共享内存中
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    QDataStream out(&buffer);
    out << str;
    int size = buffer.size();
    //3、创建共享内存
    if(!m_sharedMemory.create(size))
    {
        qDebug() << m_sharedMemory.errorString()<<"\n Unable to create shared memory segment.";
        return;
    }

    //4、将共享内存上锁
    m_sharedMemory.lock();
    char *to = (char*)m_sharedMemory.data();
    const char *from = buffer.data().data();
    //5、将进程中要共享的数据拷贝到共享内存中
    memcpy(to, from, qMin(m_sharedMemory.size(), size));
    //6、将共享内存解锁
    m_sharedMemory.unlock();
}

void Widget_PA::on_pushButton_clicked()
{
    sendStrToMemory(ui->textEdit->toPlainText());
}

工程B
widget_pb.h

#ifndef WIDGET_PB_H
#define WIDGET_PB_H
#include 
#include 
namespace Ui {
class Widget_PB;
}
class Widget_PB : public QWidget
{
    Q_OBJECT
public:
    explicit Widget_PB(QWidget *parent = 0);
    ~Widget_PB();
    QString loadStrFromMemory();
private slots:
    void on_pushButton_clicked();
private:
    Ui::Widget_PB *ui;
    QSharedMemory m_sharedMemory;
};

#endif // WIDGET_PB_H

widget_pb.cpp

#include "widget_pb.h"
#include "ui_widget_pb.h"

#include 
#include 

Widget_PB::Widget_PB(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_PB)
{
    ui->setupUi(this);
}

Widget_PB::~Widget_PB()
{
    delete ui;
}

QString Widget_PB::loadStrFromMemory()
{
    //1、设置共享内存的标志名
    m_sharedMemory.setKey("processAstr");
    //2、将共享内存与该进程绑定
    if (!m_sharedMemory.attach())
    {
        qDebug() << "Unable to attach to shared memory segment.";
        return "Unable to attach to shared memory segment.";
    }
    // 从共享内存中读取数据
    QBuffer buffer;
    QDataStream in(&buffer);
    QString str;

    //3、将共享内存上锁
    m_sharedMemory.lock();
    //4、从共享内存中取数据
    buffer.setData((char*)m_sharedMemory.constData(), m_sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    in >> str;
    //5、使用完后将共享内存解锁
    m_sharedMemory.unlock();
    //6、将共享内存与该进程分离
    m_sharedMemory.detach();
    return str;
}

void Widget_PB::on_pushButton_clicked()
{
    ui->textEdit->setText(loadStrFromMemory());
}

Qt实现IPC进程间通信-共享内存方式_第1张图片

你可能感兴趣的:(Qt经验总结,qt,开发语言)