我相信这是运算符重载的情况。我做了一个小的示范性例子:
class Test
{
public:
int i;
float f;
double d;
char empty[4];
int ii[3];
QString s;
public:
friend QDataStream& operator>>(QDataStream& in, Test& test);
friend QDataStream& operator<<(QDataStream& out, const Test& test);
};
QDataStream& operator>>(QDataStream& in, Test& test)
{
in >> test.i;
in.setFloatingPointPrecision(QDataStream::SinglePrecision);
in >> test.f;
in.setFloatingPointPrecision(QDataStream::DoublePrecision);
in >> test.d;
in.skipRawData(sizeof test.empty);
in.readRawData(reinterpret_cast<char*>(test.ii), sizeof test.ii);
in >> test.s;
return in;
}
QDataStream& operator<<(QDataStream& out, const Test& test)
{
out << test.i;
out.setFloatingPointPrecision(QDataStream::SinglePrecision);
out << test.f;
out.setFloatingPointPrecision(QDataStream::DoublePrecision);
out << test.d;
out.writeRawData(reinterpret_cast<const char*>(test.empty), sizeof test.empty);
out.writeRawData(reinterpret_cast<const char*>(test.ii), sizeof test.ii);
out << test.s;
return out;
}
然后你可以这样做:
outputStream
<< test1
<< test2
<< test3;
// ...
inputStream.startTransaction();
inputStream
>> test11
>> test22
>> test33;
inputStream.commitTransaction();
此外,这些运算符是为 Qt 基本容器( 、 等)预定QVector
义QList
的QSet
。