Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to using QShareMemory for send event betwent 2 application?
Forum Updated to NodeBB v4.3 + New Features

How to using QShareMemory for send event betwent 2 application?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 1.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Dung NguyenD Offline
    Dung NguyenD Offline
    Dung Nguyen
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Dung NguyenD 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        Dung NguyenD Offline
        Dung NguyenD Offline
        Dung Nguyen
        wrote on last edited by
        #3

        @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.

        jsulmJ 1 Reply Last reply
        0
        • Dung NguyenD Dung Nguyen

          @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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?
          }
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved