Initializing a QVector with a block of type data
-
wrote on 10 Apr 2014, 13:47 last edited by
.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.
-
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);
@ -
wrote on 10 Apr 2014, 23:31 last edited by
Excellent! I will give this a shot.
1/3