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. Initializing a QVector with a block of type data
Forum Updated to NodeBB v4.3 + New Features

Initializing a QVector with a block of type data

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kloveridge
    wrote on 10 Apr 2014, 13:47 last edited by
    #1

    .I am converting my STL usage over to QT equivalents.

    I have a binary loader which unserializes a class. To do this, I load raw binary data into a block of memory and proceed to copy it out to a class. One of the elements was originally a STL Vector. So I have a block of memory that essentially is a array of structs.

    Here's the STL version:

    @char* ChannelFloat::readBinary(char dataPtr, uint16_t elementCount)
    {
    m_floatArray.assign((AChannel::ChannelFloat
    )dataPtr, (AChannel::ChannelFloat*)dataPtr+elementCount);
    dataPtr += sizeof(AChannel::ChannelFloat) * elementCount;
    m_dirty = true;
    return dataPtr;
    }@

    I was using the .assign() to copy the raw binary data to my STL Vector

    Not sure if there is way to do this with Qt's vector class. I've been looking around and I saw a example using qCopy(). The IDE takes this code, but when I compile I get a deep error down in the bowels of qCopy().

    @char* ChannelFloat::readBinary(char dataPtr, uint16_t elementCount)
    {
    qCopy((AChannel::chanFloatStruct
    )dataPtr, (AChannel::chanFloatStruct*)dataPtr+elementCount, m_floatArray);
    dataPtr += sizeof(AChannel::chanFloatStruct) * elementCount;
    m_dirty = true;
    return dataPtr;
    }@

    I just need a way to quickly fill this QVector with raw data. I could brute force it and load it with a for..loop. But I suspect there is a better way.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 10 Apr 2014, 21:58 last edited by
      #2

      If your data is tightly packed(be mindful of alignment when you copy raw stuff) you can just memcpy it:

      @
      m_floatArray.resize(elementCount);
      memcpy(&m_floatArray[0], dataPtr,
      sizeof(AChannel::chanFloatStruct) * elementCount);
      @

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kloveridge
        wrote on 10 Apr 2014, 23:31 last edited by
        #3

        Excellent! I will give this a shot.

        1 Reply Last reply
        0

        1/3

        10 Apr 2014, 13:47

        • Login

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