Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problem with QByteArray
QtWS25 Last Chance

[SOLVED] Problem with QByteArray

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    willypuzzle
    wrote on last edited by
    #1

    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();
           }
      }
    

    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      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();
      }
      

      }@

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • W Offline
        W Offline
        willypuzzle
        wrote on last edited by
        #3

        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;
        }
        @

        1 Reply Last reply
        0
        • W Offline
          W Offline
          willypuzzle
          wrote on last edited by
          #4

          MuldeR, I have to add these lines:

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

          To insert the text in the shared memory

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

            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() ???

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • W Offline
              W Offline
              willypuzzle
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

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

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved