Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [SOLVED] Problem with QByteArray

    General and Desktop
    2
    7
    3793
    Loading More Posts
    • 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
      willypuzzle last edited by

      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 Reply Quote 0
      • M
        MuldeR last edited by

        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 Reply Quote 0
        • W
          willypuzzle last edited by

          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 Reply Quote 0
          • W
            willypuzzle last edited by

            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 Reply Quote 0
            • M
              MuldeR last edited by

              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 Reply Quote 0
              • W
                willypuzzle last edited by

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

                1 Reply Last reply Reply Quote 0
                • M
                  MuldeR last edited by

                  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 Reply Quote 0
                  • First post
                    Last post