QDataStream序列化

C字符串

        char* str = "一二三";
        QByteArray data; 
        QDataStream dt(&data, QIODevice::ReadWrite); 
        dt << str;
        QDataStream In(&data, QIODevice::ReadWrite);  


        char* strOut=NULL;
        In >> strOut;
        cout << strOut << endl;
        delete[]strOut;
        QByteArray data; 
        QDataStream dt(&data, QIODevice::ReadWrite); 
        dt << QString::fromLocal8Bit("的方法法国");
        dt << QString::fromLocal8Bit("1123");


        QDataStream In(&data, QIODevice::ReadWrite);  

        QString a;
        QString b;
        In >> a;
        In >> b;
        cout << a.toLocal8Bit().toStdString().c_str() <<","<<b.toLocal8Bit().toStdString().c_str() << endl;

QList

        QList<int> ls;
        ls << 11 << 22;
        QByteArray data; 
        QDataStream dt(&data, QIODevice::ReadWrite); 
        dt << ls;




        QDataStream In(&data, QIODevice::ReadWrite); 

        QList<int> pl;
        In >> pl; 

        for each (auto& var in pl)
        {
            cout << var << endl;
        }

QMap


        QMap<QString, int> xx;
        xx[QString::fromLocal8Bit("一")] = 11;
        xx[QString::fromLocal8Bit("二")] = 11;
        QByteArray data; 
        QDataStream dt(&data, QIODevice::ReadWrite); 
        dt << xx;

        QDataStream In(&data, QIODevice::ReadWrite); 
        QMap<QString, int> vv;
        In >> vv; 


        QMap<QString, int>::const_iterator i;
        for (i = vv.constBegin(); i != vv.constEnd(); ++i)
        {
            cout<< i.key().toLocal8Bit().toStdString() << ":" << i.value()<<endl;
        }

自定义结构体


typedef struct
{
    qint32 a;
    qint32 b;
    QString c;
}Teststruct;

inline QDataStream &operator<<(QDataStream &qDataStream,  Teststruct &dt)
{
    qDataStream << dt.a << dt.b << dt.c;
    return qDataStream;
}

inline QDataStream &operator>>(QDataStream &qDataStream,  Teststruct &dt)
{
    qDataStream >> dt.a >> dt.b >> dt.c;
    return qDataStream;
}

        Teststruct   xx = {1,2,QString::fromLocal8Bit("一二三")};

        QByteArray data; 
        QDataStream dt(&data, QIODevice::ReadWrite); 
        dt << xx;
        QDataStream In(&data, QIODevice::ReadWrite); 

        Teststruct Out;
        In >> Out;
        cout << Out.a << "," <<Out.b << ","<< Out.c.toLocal8Bit().toStdString() << endl;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值