qdatastream 如何与qvector正确关联起来
时间: 2024-02-27 20:51:35 浏览: 55
要正确关联QDataStream和QVector,可以使用QDataStream的<<和>>运算符重载。这些运算符将数据写入和读取自QDataStream。在将QVector写入QDataStream之前,需要将其转换为QList,然后将QList写入QDataStream。在从QDataStream中读取数据并将其还原为QVector时,需要先读取QList,然后将其转换为QVector。以下是一个示例:
```cpp
QVector<int> myVector;
QDataStream out(&myByteArray, QIODevice::WriteOnly);
out << static_cast<quint32>(myVector.size());
for (int i = 0; i < myVector.size(); ++i) {
out << myVector.at(i);
}
// Reading QVector from QDataStream
QDataStream in(&myByteArray, QIODevice::ReadOnly);
quint32 vectorSize;
in >> vectorSize;
QList<int> tempList;
for (quint32 i = 0; i < vectorSize; ++i) {
int tempValue;
in >> tempValue;
tempList.append(tempValue);
}
QVector<int> myVector(tempList);
```
阅读全文