I'm looking for the most simple and or elegant way to create a QBuffer in C++ Qt5.6 from void * data and long data_size.
I tried casting the void to a char pointer and using the QByteArray::fromRawData() as well as using QDataStream to fill a QByteArray. In both cases I didn't succeed.
QByteArray::QByteArray(const char *data, int size)will copy the data.QByteArray::fromRawData(const char *data, int size)will use the already existing data.Depending on your implementation, not copying the data might end up being problematic.
After you have the data in a byte array, there are several ways to go, you can directly construct a buffer:
or more likely, since you are playing audio, you might want to reusing the same buffer and use one of the following to refill it:
Lastly, there is also
void QBuffer::setData(const char *data, int size)for which you don't even need the byte array step at all.Lastly, remember that
QBufferis an IO device - it needs to be opened in order for it to work. A quick test shows that it works as expected: