Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 10:57, 8 May 2009 by Forum Nokia KB (Talk | contribs)

QSharedMemory example

From Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): Nokia N96
Platform(s): S60 3rd Edition, FP2
S60 3rd Edition FP2
Keywords: QSharedMemory
Created: (08 May 2009)
Last edited: Forum Nokia KB (08 May 2009)


This article demonstrates the inter-process communication (IPC) mechanism called Shared memory on Qt for S60 with the help of two example applications, SharedMemoryLoad and ShowSharedMem.


Shared memory is supported by Qt through the QSharedMemory class. Two processes can share the same memory segment for IPC.


The SharedMemoryLoad application loads a string into the shared memory and the ShowSharedMem application shows the string from the shared memory.

  1. Run the SharedMemoryLoad application and click the Load button.
  2. Write some text in the input dialog.
  3. Send this application to the background.
  4. Run the ShowSharedMem application and click the Show from shared memory button. The text entered in the first application is displayed.


SharedMemoryLoad is a class derived from QWidget. It contains the QSharedMemory object as a member variable:

  class SharedMemoryLoad : public QWidget
QSharedMemory sharedMem;

In the SharedMemoryLoad class constructor, set the key for the shared memory object to "MySharedMemory":

  SharedMemoryLoad::SharedMemoryLoad( QWidget *parent )
: QWidget( parent ),
sharedMem( "MySharedMemory" ) {

Loading a string into the shared memory:

  void SharedMemoryLoad::loadIntoSharedMem()
// First, test whether a shared memory segment is already attached to the process.
// If so, detach it
if (sharedMem.isAttached())
QBuffer buffer; QBuffer::ReadWrite );
QDataStream out( &buffer );
out << text;
int size = buffer.size();
if ( !sharedMem.create( size ) )
ui.LoadButton->setText(tr("Unable to create shared memory segment."));
// Write into the shared memory
char *to = (char*);
const char *from =;
memcpy( to, from, qMin( sharedMem.size(), size ) );


In the same way as SharedMemoryLoad, ShowSharedMem is derived from QWidget and owns a QSharedMemory object.

  ShowSharedMem::ShowSharedMem(QWidget *parent)
: QWidget(parent),
sharedMem("MySharedMemory") {

Loading from the shared memory:

  void ShowSharedMem::loadFromSharedMem()
if (!sharedMem.attach())
//If an attempt of reading from the shared memory before data is written
ui.showButton->setText(tr("Enter a text first"));
QBuffer buffer;
QDataStream in(&buffer);
QString text;
buffer.setData((char*)sharedMem.constData(), sharedMem.size());;
in >> text;
// As this is the last process attached to the shared memory segment
// the shared memory segment is released, destroying its contents

Sample application

Note: This example cannot be tested in the emulator because running multiple Qt applications in the emulator is not possible. Starting the second application returns KErrNotSupported(-5).

See also

Qt for S60 Known Issues

287 page views in the last 30 days.