Results 1 to 3 of 3

Thread: Share multiple segment with QSharedMemory

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Share multiple segment with QSharedMemory

    Hi every body

    i want to use QSharedMemory to share data between 2 applications using it for sharing one segment works perfectly but when i try 2 share more than one i have access just to the last one

    this my code

    sharedMemroy.cpp in the first application

    Qt Code:
    1. void SharedMemory::loadIntoSharedMem(QString memoryKey,QString &data)
    2. {
    3. sharedMem.setKey(memoryKey);
    4. if (sharedMem.isAttached())
    5. {
    6. sharedMem.detach();
    7. }
    8. if( data.length())
    9. {
    10. // load into shared memory
    11. QBuffer buffer;
    12. buffer.open(QBuffer::ReadWrite);
    13. QDataStream out(&buffer);
    14. out << data;
    15. int size = buffer.size();
    16.  
    17. if (!sharedMem.create(size)) {
    18. qDebug()<<"Unable to create shared memory segment."<<sharedMem.isAttached()<<" "<<sharedMem.error();
    19.  
    20. }
    21.  
    22. sharedMem.lock();
    23. char *to = (char*)sharedMem.data();
    24. const char *from = buffer.data().data();
    25. memcpy(to, from, qMin(sharedMem.size(), size));
    26. sharedMem.unlock();
    27. }
    28. else
    29. {
    30. qDebug()<< "no data to share"
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    and i main

    Qt Code:
    1. SharedMemory sh;
    2. sh.loadIntoSharedMem("memo1",data1 );
    3. sh.loadIntoSharedMem("memo2",data2 );
    4. sh.loadIntoSharedMem("memo3",data3 );
    To copy to clipboard, switch view to plain text mode 


    and on showSharedData.cpp in the seconde application


    Qt Code:
    1. QString ShowSharedMemory::loadFromSharedMem(QString memoryKey)
    2. {
    3. sharedMem.setKey(memoryKey);
    4. if (!sharedMem.attach())
    5. {
    6. qDebug()<<"Unable to load!";
    7. return"";
    8. }
    9.  
    10. QBuffer buffer;
    11. QDataStream in(&buffer);
    12. QString text;
    13.  
    14. sharedMem.lock();
    15. buffer.setData((char*)sharedMem.constData(), sharedMem.size());
    16. buffer.open(QBuffer::ReadOnly);
    17. in >> text;
    18. sharedMem.unlock();
    19.  
    20. sharedMem.detach();
    21. return text;
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    and in main

    Qt Code:
    1. data1=showMemory.loadFromSharedMem("memo1");
    2. data2=showMemory.loadFromSharedMem("memo2");
    3. data3=showMemory.loadFromSharedMem("memo3");
    To copy to clipboard, switch view to plain text mode 


    thanks for your help
    Last edited by boumacmilan; 21st January 2014 at 15:21.

  2. #2
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Share multiple segment with QSharedMemory

    i resolve it by mading 3 object SharedMemory, i know its not a good methode , but it works
    (sorry for my bad english)

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Share multiple segment with QSharedMemory

    Quote Originally Posted by boumacmilan View Post
    i resolve it by mading 3 object SharedMemory, i know its not a good methode
    I don't think there is an alternative, so it is the right way to do it.

    QSharedMemory is a bit like a smart pointer, so you need to keep it around as long as you want to work with the shared memory it is associated with.

    Cheers,
    _

Similar Threads

  1. Share QGLContext among Multiple Threads (4.7.1)
    By JediMaster in forum Newbie
    Replies: 0
    Last Post: 20th June 2013, 13:10
  2. Replies: 1
    Last Post: 8th May 2013, 11:09
  3. Segment fault
    By kenchan in forum Qt Programming
    Replies: 14
    Last Post: 25th December 2012, 11:30
  4. I need a 16 segment LCD display control????
    By phoenixcomm in forum Newbie
    Replies: 2
    Last Post: 2nd July 2012, 00:26
  5. Replies: 2
    Last Post: 25th March 2010, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.