PDA

View Full Version : QSharedMemory, signals and QBuffer



sky
22nd November 2011, 12:52
Hi,

I am trying to get signals when a QSharedMemory is written to. I tried it a few times in the folowing way:




QSharedMemory *sm = new QSharedMemory("key", this);
QByteArray *ba;
QBuffer *buff;
if(sm->create(1024))
{
ba = new QByteArray();
ba->setRawData((char*)sm->data(), sm->size());
buff = new QBuffer(ba, this);
connect(buff, SIGNAL(readyRead()), this, SLOT(bufferReadyRead()));

memset(sm->data(), NULL, sm->size());
QByteArray response = myDomDoc.toByteArray();

sm->lock();
buff->open(QIODevice::ReadWrite);
memcpy((char*)sm->data(), response.data(), sm->size()-1);
buff->close();
// Here I want the QBuffer's readyRead() signal to emit
sm->unlock();
}


My attempt might be a little contorted but I hope you get the idea. I am trying to wrap a QBuffer around a QSharedMemory data so that I can get a signal when somebody writes to the QSharedMemory.

Is this a good way to do this? Is there a better way to do it? If not what is best way to get a signal when my QSharedMemory is written to.

Best regards.

sky
23rd November 2011, 06:06
I think the problem is that as soon as I write to the shared memory data the QByteArray implements a copy-on-write and therefore the QBuffer does not detect something written to its data.

am I right?