How to using QShareMemory for send event betwent 2 application?



  • Hello every body,
    I am a newbie and I want to setup an Inter Process Communication into two process.
    I using Qt 5.7 and QsharedMemory in Window

    in the first app

    sentoservice.hbolded text

    #ifndef SENDTOSERVICE_H
    #define SENDTOSERVICE_H

    #include <QObject>
    #include <qsharedmemory.h>

    class SendToService:public QObject
    {
    Q_OBJECT

    public:
    // Initialize shared memory
    // void InitialSharedMemory();
    // Write to shared memory
    void writeToShareMemory(QString event);

    private:
    // create shaed memory
    QSharedMemory sharedMemory;

    };

    #endif // SENDTOSERVICE_H

    sentoservice.cppbolded text

    #include "sendtoservice.h"
    #include <QBuffer>
    #include <QtCore/QDebug>
    #include <QSharedMemory>
    #include<QDataStream>

    void SendToService::writeToShareMemory(QString event)
    {
    sharedMemory.setKey("server1234");
    sharedMemory.attach();
    if (sharedMemory.isAttached())
    {
    sharedMemory.detach();
    }

    QBuffer buffer;
    buffer.open( QBuffer::ReadWrite );
    QDataStream out( &buffer );
    
    out << event;
    

    int size = buffer.size();

    // Write into the shared memory
    
     sharedMemory.lock();
    qDebug() << "Writing data to buffer: " << event;
    char *to = (char*)sharedMemory.data();
    const char *from = buffer.data();
    

    // qDebug()<< &from;
    qDebug() << "Pointer In Shared Memory is: " << &to;
    qDebug() << "Pointer In Buffer is: " << &from;
    memcpy( to, from, qMin( sharedMemory.size(), size) );

    sharedMemory.unlock();
    

    }

    main.cppbolded text

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    SendToService loadData;
    

    // loadData.InitialSharedMemory();
    loadData.writeToShareMemory("play");

    view.rootContext()->setContextProperty("receiver",&receiverdata);
    view.rootContext()->setContextProperty("audioController", &data);
    view.setSource(QUrl("qrc:/audio.qml"));
    view.show();
    
    
    return app.exec();
    

    }

    in the Second app

    dialog.hbolded text

    #ifndef RECIEFROMQML_H
    #define RECIEFROMQML_H

    #include <QObject>
    #include <qsharedmemory.h>
    #include <qdialog.h>
    #include "ui_dialog.h"

    class Dialog:public QDialog
    {
    Q_OBJECT

    public :

    Dialog(QWidget *parent = 0);
    

    public slots:

    QString loadFromMemory();
    

    private:
    Ui::Dialog ui;
    QSharedMemory sharedMemory;
    };

    #endif // RECIEFROMQML_H

    dialo.cppbolded text

    #include "dialog.h"
    #include <QBuffer>
    #include <QtCore/QDebug>
    #include<QSharedMemory>

    Dialog::Dialog(QWidget *parent)
    : QDialog(parent), sharedMemory("QSharedMemoryExample")
    {
    ui.setupUi(this);

    connect(ui.loadFromSharedMemoryButton,
            SIGNAL(clicked()),
            SLOT(loadFromMemory()));
    

    }

    QString Dialog::loadFromMemory()
    {
    sharedMemory.setKey("server1234");
    sharedMemory.registerUserData();

    QBuffer buffer;
    QDataStream in(&buffer);
    //QByteArray in;
    
    QString event;
    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    in >> event;
    sharedMemory.unlock();
    qDebug()<< event;
    if (event=="play")
    {
    ui.label->setText(tr("playing music"));
    }
    else
    {
        ui.label->setText(tr("can't play"));
    }
    return event;
    

    }

    main .cppbolded text

    #include "mainwindow.h"
    #include "reciefromqml.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    Dialog dialog;
      dialog.show();
    
    return a.exec();
    

    }

    I running 2 app using 2 QT but I can't send message betwent 2 process.
    Can you help me fix my code ?
    Thank you very much!


  • Lifetime Qt Champion

    Hi,

    In your writeToShareMemory function you detach at the top if attached successfully. This looks a bit strange.

    Out of curiosity, why QSharedMemory rather than e.g. QLocalServer/Socket ?



  • @SGaist
    thanks guy!
    because i write code for embeded sys , i need the speed for communication between a lot of app in the same system.


  • Moderators

    @Dung-Nguyen As @SGaist said: why do you detach after attaching?

    void SendToService::writeToShareMemory(QString event)
    {
    sharedMemory.setKey("server1234");
    sharedMemory.attach();
    if (sharedMemory.isAttached())
    {
    sharedMemory.detach(); // WHY?
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.