[SOLVED] Problem with QByteArray



  • I have this code but it doesn't work properly because 'from' pointer content is set to "0\0".
    'hold' variable is set properly.
    What's wrong?

    @
    QSharedMemory sharedMemory;
    QString uniqueID = "abcde";
    sharedMemory.setKey(uniqueID);

        if(sharedMemory.attach()){
    
            QBuffer buffer;
            buffer.open( QBuffer::ReadWrite );
            QDataStream out( &buffer );
            out << "qadvice, raise window!";
            int size = buffer.size();
    
            if (sharedMemory.lock()){
              char *to = (char*)sharedMemory.data();
              QByteArray hold = buffer.data();
              char *from = new char[hold.size() + 1];
              strcpy(from, hold.data());
              memcpy( to, from, qMin( sharedMemory.size(), (size+1) ) );
              delete [] from;
              sharedMemory.unlock();
           }
      }
    

    @



  • What exactly are you trying to do?

    Why not simply:
    @QSharedMemory sharedMemory;
    QString uniqueID = "abcde";
    sharedMemory.setKey(uniqueID);

    if(sharedMemory.attach())
    {
    QBuffer buffer;
    buffer.open( QBuffer::ReadWrite );

    if (sharedMemory.lock())
    {
        QByteArray &data = buffer.data();
        memcpy(
            sharedMemory.data(),
            data.constData(),
            qMin(data.size() + 1, sharedMemory.size())
        );
        sharedMemory.data()[sharedMemory.size() - 1] = '\0';
        sharedMemory.unlock();
    }
    

    }@



  • I resolved the problem, I discovered that 'hold' variable holds 3 zeros at first three positions, and the number of elements in the fourth position.

    So to recover the string I had to cut the first 4 chars of the variable 'hold', with this function:

    @
    const char* analizeStream(const QByteArray& originalArray, int* size){
    size = originalArray.size() - 4;
    char
    toReturn = new char[*size];
    for (int i=0; i < (*size); i++){
    char c = originalArray[i+4];
    toReturn[i] = c;
    }
    return toReturn;
    }
    @



  • MuldeR, I have to add these lines:

    @
    QDataStream out( &buffer );
    out << "qadvice, raise window!";
    @

    To insert the text in the shared memory



  • If that's all, why not:

    @static const QString text("qadvice, raise window!");

    if(sharedMemory.attach())
    {
    //QBuffer buffer;
    //buffer.open( QBuffer::ReadWrite );

    if (sharedMemory.lock())
    {
        QByteArray data = text.toUtf8();
        memcpy(
            sharedMemory.data(),
            data.constData(),
            qMin(data.size() + 1, sharedMemory.size())
        );
        sharedMemory.data()[sharedMemory.size() - 1] = '\0';
        sharedMemory.unlock();
    }
    

    }@

    Why do you need the QBuffer, the QDataStream and the strcpy() ???



  • So QBuffer buffer is useless ....
    I read it on a tutorial http://www.developer.nokia.com/Community/Wiki/QSharedMemory_example



  • Wouldn't say it's "useless". Maybe just not needed/helpful in your specific case, as far as I can see.

    The purpose of QBuffer is that you get a QIODevice backed by a QByteArray as its internal buffer. So if you need to access a QByteArray through a QIODevice-style interface, then QBuffer would be the way to go.

    The same way, wrapping your QBuffer into a QDataStream is helpful if you need to serialize a sequence of elements of various types into a platform-independent byte stream. Just for pumping a simple string into a QByteArray, using a QBuffer+QDataStream is like taking a sledgehammer to crack a nut ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.