如何在QT中的QShared内存中写入多个数据

时间:2013-12-11 04:58:23

标签: c++ qt

我想写4到5个变量给QSharedMemory,但我不知道读写这个。

我有五个不同数据类型的变量A,B,C,D,E。在下面的程序中,我将变量A写入共享内存。如何写其他变量也一样?

该程序写入共享内存。

                 if(sharedMem.isAttached())
                   {
                   sharedMem.detach();
                   qDebug() <<"Here shared";
                   }

          QString text = QString::number(A);   // A is Passed

          QBuffer buffer;
          buffer.open( QBuffer::ReadWrite );
          QDataStream out( &buffer );
          out << text;

          int size1 = buffer.size();

          if ( !sharedMem.create( size1 ) )
          {
          qDebug() << "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(), size1 ) );
          sharedMem.unlock();

如果我传递所有5个变量,如何使用以下程序检索这些变量。

此程序从共享内存中读取。

  if (!sharedMem.attach())
       {

        ui->label->setText(tr("Unable to load!"));
        return;
       }
QBuffer buffer;
QDataStream in(&buffer);
QString text;

sharedMem.lock();
char* pointer = (char*)sharedMem.constData();
buffer.setData((char*)sharedMem.constData(), sharedMem.size());
buffer.open(QBuffer::ReadOnly);
in >> text;                                     // Reading A
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();

编辑:

我尝试使用QStringlist传递所有变量,但我收到错误。

1 个答案:

答案 0 :(得分:0)

我通过在第一个程序中连接QString并在第二个程序中使用QStringlist Split函数解决了这个问题。

该程序写入共享内存。

   if(sharedMem.isAttached())
   {
   sharedMem.detach();
   qDebug() <<"Here shared";
   }
   // I changed here . now A & B is Passed .

  QString text = QString::number(A)+ "," + QString::number(B) ;   


  QBuffer buffer;
  buffer.open( QBuffer::ReadWrite );
  QDataStream out( &buffer );
  out << text;

  int size1 = buffer.size();

  if ( !sharedMem.create( size1 ) )
  {
  qDebug() << "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(), size1 ) );
  sharedMem.unlock();

此程序从共享内存中读取。

  if (!sharedMem.attach())
  {

  ui->label->setText(tr("Unable to load!"));
  return;
  }
  QByteArray read1;               // used ByteArray Change 1
  QBuffer buffer;
  QDataStream in(&buffer);
  QString text = QString(read);   // Change 2

  QStringList strlist = text.split(","); // Change 3
  int A = strlist.at(0).toInt(0);
  int B = strlist.at(1).toInt(0);



  sharedMem.lock();
  char* pointer = (char*)sharedMem.constData();
  buffer.setData((char*)sharedMem.constData(), sharedMem.size());
  buffer.open(QBuffer::ReadOnly);
  in >> text;                                     // Reading A
  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();