qt序列化自定义对象,计算Qt中对象的序列化大小

How can I know the size of qt data types in bytes; including QString objects if these data types were written on some QFile. I have to implement sizeOf() function in the Student class as below; something like we sizeof(struct student) in C.

Student class

#include

class Student

{

public:

QString name,fname;

quint8 age,weight,clss;

Student(){

}

/*the following function should return the size of this object in bytes;

I will use QDataStream & operator<

function to write data to a QFile */

qint16 sizeOf()

{

// needs implementation;

}

};

QDataStream & operator<

{

out<

return out;

}

QDataStream & operator>>(QDataStream &in, Student &f)

{

in>>f.name>>f.fname>>f.age>>f.weight>>f.clss>>f.next;

return in;

}

I know that data can be read with QDataStream & operator>>(QDataStream &in, Student &f); but I want to know size also for some other cases.

This does not give me a valid size on file. It seems Qt adds some extra bits while serializing; possibly for endian-ness independency on different platforms. Actual size is always more than returned by sizeOf() function

qint16 sizeOf()

{

qint16 size=0;

size+=sizeof(quint8)*3; // size of age, weight and clss

//variables all have type quint8

size+=name.size()*16; // number of characters in string

// multiply with 16 bit QChar

size+=fname.size()*16;

return size;

}

I am using QFile, QDataStream api. Qt version 4.8 on Windows 8.

解决方案

The size which sizeof will give you does not reflect the actual size an object might have. For example, the sizeof a QString in a 32 bit build will always be 4 bites, regardless how long the actual string is.

This sizeof operator includes stuff that doesn't need to be serialized, like the object's vtable pointer, and does not account for the size of dynamically allocated resources for that object.

You can easily determine the serializable size, just use a QDataStream, from the device() get the pos() before, input the object in the data stream and compare with the pos() afterwards.

Also, this line is clearly wrong: size+=sizeof(quint8*3) it will not give you three times the size of a byte. It will give you the size of an int, which is how the result is promoted after the multiplication.

Here is a nifty little class you can use for the task:

class SerialSize {

public:

SerialSize() : stream(&data) { data.open(QIODevice::WriteOnly); }

template

quint64 operator ()(const T & t) {

data.seek(0);

stream << t;

return data.pos();

}

private:

QBuffer data;

QDataStream stream;

};

Then use it:

SerialSize size;

qDebug() << size(QString("a")); // 6

qDebug() << size(QString("aa")); // 8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用QDataStream序列化自定义类的QList时,可能会遇到C2679错误。这个错误表示在编译时,编译器无法找到一个可以匹配的操作符。 为了解决这个问题,你需要在你的自定义重载以下两个操作符: 1. QDataStream& operator<<(QDataStream& out, const YourClass& obj) 2. QDataStream& operator>>(QDataStream& in, YourClass& obj) 这两个操作符分别用于序列化和反序列化自定义类的对象。 下面是一个简单的例子来说明如何重载这两个操作符: ```c++ class MyClass { public: QString name; int age; friend QDataStream& operator<<(QDataStream& out, const MyClass& obj) { out << obj.name << obj.age; return out; } friend QDataStream& operator>>(QDataStream& in, MyClass& obj) { in >> obj.name >> obj.age; return in; } }; Q_DECLARE_METATYPE(MyClass); // 声明MyClass是Qt元类型 QDataStream& operator<<(QDataStream& out, const QList<MyClass>& list) { out << quint32(list.size()); for (const MyClass& obj : list) { out << obj; } return out; } QDataStream& operator>>(QDataStream& in, QList<MyClass>& list) { quint32 size; in >> size; list.clear(); for (quint32 i = 0; i < size; i++) { MyClass obj; in >> obj; list.append(obj); } return in; } ``` 在上面的例子,我们重载了MyClass类的序列化和反序列化操作符,并且还重载了QList<MyClass>的序列化和反序列化操作符。注意,我们还需要声明MyClass是Qt元类型,这样才能在使用QVariant和QMetaProperty时使用它。 现在你可以使用QDataStream来序列化和反序列化自定义类的QList了: ```c++ QList<MyClass> list; list.append(MyClass{"Alice", 25}); list.append(MyClass{"Bob", 30}); QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream << list; QList<MyClass> newList; stream.device()->seek(0); stream >> newList; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值