简体   繁体   English

来自空指针和内存大小的QBuffer

[英]QBuffer from void pointer and memory size

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 . 我正在寻找从void * datalong data_size在C ++ Qt5.6中创建QBuffer的最简单或最优雅的方法。

I tried casting the void to a char pointer and using the QByteArray::fromRawData() as well as using QDataStream to fill a QByteArray . 我尝试将void强制转换为char指针,并使用QByteArray::fromRawData()以及使用QDataStream填充QByteArray In both cases I didn't succeed. 在两种情况下我都没有成功。

QByteArray::QByteArray(const char *data, int size) will copy the data. QByteArray::QByteArray(const char *data, int size)将复制数据。

QByteArray::fromRawData(const char *data, int size) will use the already existing data. QByteArray::fromRawData(const char *data, int size)将使用已经存在的数据。

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: 将数据存储在字节数组中之后,有几种处理方法,您可以直接构造一个缓冲区:

QBuffer(QByteArray *byteArray, QObject *parent = Q_NULLPTR)

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: 或更可能的是,由于您正在播放音频,因此您可能想重新使用相同的缓冲区并使用以下方法之一重新填充它:

setBuffer(QByteArray *byteArray)
setData(const QByteArray &data)

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. 最后,还有一个void QBuffer::setData(const char *data, int size) ,您甚至不需要它的字节数组步骤。

Lastly, remember that QBuffer is an IO device - it needs to be opened in order for it to work. 最后,请记住, QBuffer是一个IO设备-需要打开它才能使其工作。 A quick test shows that it works as expected: 快速测试表明它可以正常工作:

char data[] = {1, 2, 3};
void * vdata = data;
QBuffer buffer;
buffer.setData((char *)vdata, sizeof(data));
buffer.open(QIODevice::ReadOnly);
qDebug() << buffer.readAll(); // outputs "\x01\x02\x03"

Since you are asking for an elegant way to create a QBuffer from data and data_size, I would simply use this: 由于您正在寻求一种优雅的方法来根据data和data_size创建QBuffer,因此我将简单地使用以下方法:

QBuffer buffer;
buffer.setData(static_cast<const char*>(data), data_size);

Note: this copies the memory pointed to by data . 注意:这将复制 data指向的内存。


I have no idea why you are unable to succeed, maybe you forgot to add the data_size as size argument for QByteArray::fromRawData ? 我不知道为什么您无法成功,也许您忘了添加data_size作为QByteArray::fromRawData大小参数? In that case strlen(data) would be used to calculate the size. 在这种情况下, strlen(data)将用于计算大小。

And if you are using something like this: 如果您使用的是这样的内容:

QByteArray byteArray = QByteArray::fromRawData(static_cast<const char*>(data), data_size);
QBuffer buffer(&byteArray);

Then the byteArray must remain valid until the QBuffer is destroyed. 然后, byteArray必须保持有效,直到销毁QBuffer。 And since QByteArray::fromRawData(...) does not copy, the memory pointed to by data must remain valid as well. 而且由于QByteArray::fromRawData(...)无法复制,因此data指向的内存也必须保持有效。 Failing to meet that requirement would also explain any failure. 未能满足该要求也将解释任何失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM