I met the same question a few days ago. I couldn't find the answers on the Internet, so I tried to write the codes by myself. The method is to copy the raw data of the images to and from the shared memory directly. This method is very very fast.
The function for sending the images:
//The image-file names are stored in a QStringList object
void SendImage(QStringList fileList)
{
if (sharedMemory.isAttached())
sharedMemory.detach();
int count = fileList.count();
int totalSize = 0;
QList<QImage> images;
//Load the images
for (int i = 0; i < count; i++)
{
QImage image(fileList.at(i));
images.append(image);
totalSize += image.sizeInBytes();
}
//Total size needed for shared memory
totalSize += (count * 5 + 1) * sizeof(int);
if (!sharedMemory.create(totalSize)) {
throw QString( "Shared memory cannot be allocated!");
return;
}
sharedMemory.lock();
//to points to the target space to write
uchar *to = (uchar *)sharedMemory.data();
//from points to the source to write
uchar *from = (uchar *)&count;
//Write the count of the images from the first byte of the shared memory
memcpy(to, from, sizeof(int));
//Move the pointer forward
to += sizeof(int);
//Write the images one by one
for (int i = 0; i < count; i++) {
//Write the total size of the image and move the pointer forward
int sizeInBytes = images.at(i).sizeInBytes();
from = (uchar *)&sizeInBytes;
memcpy(to, from ,sizeof(int));
to += sizeof(int);
//Write the width of the image and move the pointer forward
int width = images.at(i).width();
from = (uchar *)&width;
memcpy(to, from ,sizeof(int));
to += sizeof(int);
//Write the height of the image and move the pointer forward
int height = images.at(i).height();
from = (uchar *)&height;
memcpy(to, from ,sizeof(int));
to += sizeof(int);
//Write the image format of the image and move the pointer forward
int imageFormat = (int)images.at(i).format() ;
from = (uchar *)&imageFormat;
memcpy(to, from ,sizeof(int));
to += sizeof(int);
//Write the bytes per line of the image and move the pointer forward
int bytesPerLine = images.at(i).bytesPerLine();
from = (uchar *)&bytesPerLine;
memcpy(to, from ,sizeof(int));
to += sizeof(int);
//Write the raw data of the image and move the pointer forward
from = (uchar *)images.at(i).bits();
memcpy(to, from, sizeInBytes);
to += sizeInBytes;
}
sharedMemory.unlock();
}
My code for receiving the images:
QList<QImage> * FetchImages()
{
QList<QImage> * imageList = new QList<QImage>;
if (!sharedMemory.attach())
{
throw QString("The shared memory cannot be attached!");
return imageList;
}
sharedMemory.lock();
//from points to the first byte of the shared memory
uchar * from = (uchar *)sharedMemory.data();
//Get the count of the images and move the pointer forward
int count;
count = *(int *)from;
from += sizeof(int);
//Get the images one by one
for (int i = 0; i < count; i++)
{
//Get the size of the image and move the pointer forward
int sizeInBytes = *(int *)from;
from += sizeof(int);
//Get the width of the image and move the pointer forward
int width = *(int *)from;
from += sizeof(int);
//Get the height of the image and move the pointer forward
int height = *(int *)from;
from += sizeof(int);
//Get the image format of the image and move the pointer forward
int imageFormat = *(int *)from;
from += sizeof(int);
////Get the bytes per line of the image and move the pointer forward
int bytesPerLine = *(int *)from;
from += sizeof(int);
//Generate an image using the raw data and move the pointer forward
QImage image(from, width, height, bytesPerLine, (QImage::Format)imageFormat);
from += sizeInBytes;
//It is very important here and I spent much time.
//A new image object "image1" must be deeply copied from "image" before appended to the imageList.
//Because the raw data of "image" use the space of the shared memory, which will not be accessed after the shared memory is detached.
//The raw data of the newly constructed "image1" use the memory of its own process.
QImage image1 = image.copy(0, 0, width, height);
imageList -> append(image1);
}
sharedMemory.unlock();
sharedMemory.detach();
return imageList;
}