×
Namespaces

Variants
Actions

QSharedMemory example

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia N96
Compatibility
Platform(s): S60 3rd Edition, FP2
Symbian
S60 3rd Edition FP2
Article
Keywords: QSharedMemory, IPC
Created: User:Technical writer 1 (08 May 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This article demonstrates the inter-process communication (IPC) mechanism called Shared memory on Qt using the example applications SharedMemoryLoad and ShowSharedMem.

Description

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

Solution

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

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

  class SharedMemoryLoad : public QWidget
{
Q_OBJECT
...
private:
 
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())
{
sharedMem.detach();
}
 
...
 
QBuffer buffer;
buffer.open( 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."));
return;
}
 
// Write into the shared memory
sharedMem.lock();
char *to = (char*)sharedMem.data();
const char *from = buffer.data().data();
memcpy( to, from, qMin( sharedMem.size(), size ) );
sharedMem.unlock();
}


ShowSharedMem

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"));
return;
}
 
QBuffer buffer;
QDataStream in(&buffer);
QString text;
 
sharedMem.lock();
buffer.setData((char*)sharedMem.constData(), sharedMem.size());
buffer.open(QBuffer::ReadOnly);
in >> text;
sharedMem.unlock();
 
// As this is the last process attached to the shared memory segment
// the shared memory segment is released, destroying its contents
sharedMem.detach();
}

Sample application

File:QSharedMemoryExample.zip

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

This page was last modified on 11 October 2012, at 04:18.
238 page views in the last 30 days.
×