精华内容
下载资源
问答
  • Qt序列化

    2019-05-03 15:42:53
    转载:http://www.cnblogs.com/findumars/p/6568869.html
    展开全文
  • I am programming a GUI with Qt library. In my GUI I have a huge std::map."MyType" is a class that has different kinds of fields.I want to serialize the std::map. How can I do that? Does Qt provides us...

    I am programming a GUI with Qt library. In my GUI I have a huge std::map.

    "MyType" is a class that has different kinds of fields.

    I want to serialize the std::map. How can I do that? Does Qt provides us with neccesary features?

    解决方案

    QDataStream handles a variety of C++ and Qt data types. The complete list is available at http://qt-project.org/doc/qt-4.8/datastreamformat.html. We can also add support for our own custom types by overloading the << and >> operators. Here's the definition of a custom data type that can be used with QDataStream:

    class Painting

    {

    public:

    Painting() { myYear = 0; }

    Painting(const QString &title, const QString &artist, int year) {

    myTitle = title;

    myArtist = artist;

    myYear = year;

    }

    void setTitle(const QString &title) { myTitle = title; }

    QString title() const { return myTitle; }

    ...

    private:

    QString myTitle;

    QString myArtist;

    int myYear;

    };

    QDataStream &operator<

    QDataStream &operator>>(QDataStream &in, Painting &painting);

    Here's how we would implement the << operator:

    QDataStream &operator<

    {

    out << painting.title() << painting.artist()

    << quint32(painting.year());

    return out;

    }

    To output a Painting, we simply output two QStrings and a quint32. At the end of the function, we return the stream. This is a common C++ idiom that allows us to use a chain of << operators with an output stream. For example:

    out << painting1 << painting2 << painting3;

    The implementation of operator>>() is similar to that of operator<

    QDataStream &operator>>(QDataStream &in, Painting &painting)

    {

    QString title;

    QString artist;

    quint32 year;

    in >> title >> artist >> year;

    painting = Painting(title, artist, year);

    return in;

    }

    This is from: C++ GUI Programming with Qt 4 By Jasmin Blanchette, Mark Summerfield

    展开全文
  • 功能:Qt序列化与发序列化的实现(QDataStream) 用途:UI界面自定义数据结构的保存于读取 ```cpp struct Body { double weight; double height; }; //结构体 struct People { int age; Body dBody;//结构体 ...

    功能:Qt序列化与发序列化的实现(QDataStream)
    用途:UI界面自定义数据结构的保存于读取

    ```cpp
    struct Body
    
    {
        double weight;
        double height;
    };
    //结构体
    struct People
    {
        int age;
        Body dBody;//结构体
        vector<QString> vecfamily;//vector
        //序列化
        friend QDataStream &operator<<(QDataStream& input,const People &iteam)
        {
            //vector 数据类型需要用vector<People>::fromStdVector 转一下
            //如果是QList则不需要直接插入
            QVector<QString> strvecfamily = QVector<QString>::fromStdVector(iteam.vecfamily);
            input << iteam.age << iteam.dBody.height << iteam.dBody.weight\
                  << strvecfamily;
            return input;
        }
        //反序列化
        friend QDataStream &operator>>(QDataStream& output,People& iteam)
        {
            QVector<QString> vecfamily;
            output >> iteam.age >>iteam.dBody.height >> iteam.dBody.weight >> vecfamily;
            iteam.vecfamily = vecfamily.toStdVector();
            return output;
        }
    };
    数据的使用
    People p1;
        p1.age = 12;
        p1.dBody.height = 150.8;
        p1.dBody.weight = 50;
        p1.vecfamily.push_back("sister");
        p1.vecfamily.push_back("mother");
        p1.vecfamily.push_back("father");
        QFile file("./stream.bat");
        if(!file.open(QIODevice::WriteOnly))
        {
            return ;
        }
        QDataStream input(&file);
        input << p1;
        file.close();
        People p2;
        QFile file1("./stream.bat");//.bat文件为二进制文件
        if(!file1.open(QIODevice::ReadOnly))
        {
            return ;
        }
        QDataStream output(&file1);
        output << p2;
        file.close();
    
        output >> p2;
        qDebug()<<p2.age<<p2.dBody.height<<p2.dBody.weight\
               <<p2.vecfamily;//结果12 150.8 50 std::vector("sister", "mother", "father")
    
    

    原文链接:https://www.cnblogs.com/that-boy-done/p/10739097.html
    非原创,侵权必删!

    展开全文
  • Qt序列化和反序列化

    千次阅读 2019-05-15 15:10:42
    定义: ...Qt中实现对象序列化的类是QDataStream,写二进制到流中 QFile file("file.dat"); //创建一个文档 file.open(QIODevice::WriteOnly);//打开并只写 QDataStream out(&file); //序列...

    定义:

    对象转换为二进制——>序列化
    二进制转换为对象——>反序列化

    序列化:

    Qt中实现对象序列化的类是QDataStream,写二进制到流中

    QFile file("file.dat");         //创建一个文档
    file.open(QIODevice::WriteOnly);//打开并只写
    QDataStream out(&file);         //序列化文档
    out << QString("the answer is");//将字符串写入文档中,实际在该文档中为二进制
    out << (qint32)42;
    

    反序列化:

    从流中读入二进制

    QFile file("file.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);    // read the data serialized from the file
    QString str;
    qint32 a;
    in >> str >> a; 
    
    • 提醒:当Qt的Qdatastream 满足不了定义的数据对象的序列化和反序列化时
      需要对已有的数据对象进行<<和>>的重载函数实现。
    展开全文
  • 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; someth...
  • Qt序列化数据

    2020-08-15 18:25:46
    Qt提供的QDataStream可以序列化一些数据,如下: bool, float, double, const char * qint8, qint16, qint32, qint64, quint8, quint16, quint32, quint64 QString, QTime, QTransform, QUrl, QVariant QBitArray, ...
  • Qt 序列化按钮

    2020-10-29 14:51:22
    通过序列化按钮,可以极大减少代码量。 通过QList容器存放12个按钮 //序列化通道按钮 QList<QPushButton*> pushButtonList; pushButtonList = pushButtonList << ui->target1_pushButton <&...
  • QT序列化与网络传输

    2020-11-22 21:53:15
    介绍QT序列化与网络传输之间的关系!
  • Qt 序列化操作

    千次阅读 2015-09-10 10:13:43
    这里主要分两类,即:QT中原生的数据类型,例如:QString、QMap、QHash等,这对这种原生数据类型的序列化,我们不需要做其它额外的操作,直接就可以序列化到文件中。还有一类特殊的就是我们自己定义的数据结构或类,...
  • Qt 序列化对象

    千次阅读 2013-08-30 09:48:13
    Qt序列化数据用的类是QDataStream,但这个类只能序列化基本类型和一部分Qt定义的类,对于自定义类需要自己编写序列化函数。 在自定义类中声明友元函数,并实现: class CKDrawBase: { public:   friend ...
  • C++/Qt 序列化操作

    2015-12-15 16:27:10
    序列化操作的含义顾名思义,即按一定的次序将数据保存到存储设备中,或者从存储设备中读取一定次序的数据,且数据类型要保持一致(即读出的数据是我知道的类型)。过程知道了,那怎么做呢?我是借助Qt框架实现的一个...
  • qt序列化 (三)

    2016-06-07 13:53:20
    When we try to compile the above code, we will receive a cryptic error similar to 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>'. That's because a QList<Bookmark> cannot be converted into a ...
  • qt序列化 (一)

    千次阅读 2016-06-07 13:49:10
    the Qt framework doesn't have a built-in serialization support in the same sense as, for example, .NET or Java, it provides at least three mechanisms that can make storing and reading data easier: ...
  • // 序列化 inline QDataStream &operator(QDataStream &o,const DataList &d) { return o ; } // 序列化 inline QDataStream &operator(QDataStream &o,const Data &d) { return o ; } // 反序列化 inline ...
  • 【大话QT之七】QT序列化操作

    万次阅读 2014-05-09 21:50:19
    这里主要分两类,即:QT中原生的数据类型,例如:QString、QMap、QHash等,这对这种原生数据类型的序列化,我们不需要做其它额外的操作,直接就可以序列化到文件中。还有一类特殊的就是我们自己定义的数据结构或类,...
  • Google Protobuf基于Qt开发序列化与反序列化用QUdpSocket传输并显示。实例。 具体可查看了解:https://blog.csdn.net/automoblie0/article/details/101363526
  • Qt自定义结构序列化

    2021-01-18 23:02:17
    Qt自定义结构序列化与反序列化,对应文章:https://lizhifun.blog.csdn.net/article/details/112796140
  • Qt下实现序列化与反序列化

    千次阅读 2018-12-18 14:22:31
    写了一个工具类MyDatastream,使用时需要实例化对象,调用相应的序列化或反序列化函数即可,读者可以根据自己定制的结构体自行扩展。例子中的结构体如下: struct UserInfo { unsigned char id[20]; int idint; ...
  • Qt自定义序列化

    2021-05-30 21:35:54
    本文转载自...q=QMap%E5%BA%8F%E5%88%97%E5%8C%96&...QDateStream类 在Qt中,提供了QDataStream类为我们的程序提供了读写二进制数据的能力。 QDataStream类实现了序列化C++的基本数据类型的功能,
  • 定义: ...Qt中实现对象序列化的类是QDataStream,写二进制到流中 QFile fileout("file.dat"); //创建一个文档 fileout.open(QIODevice::WriteOnly | QIODevice::Text);//打开并只写 QDataSt...
  • Qt序列化自定义对象

    千次阅读 2014-09-29 15:03:13
    Qt序列化数据的类是QDataStream,但这个类只能序列化基本类型和一部分Qt定义的类,对于自定义类需要自己编写序列化函数。 在自定义类中声明友元函数,并实现: class CKDrawBase: { public:   friend ...
  • 定义 序列化:将对象或数据结构转换为二...Qt序列化与反序列化 Qt中使用QDataStream类实现对象序列化序列化: QFile file("file.rx"); //定义文件路径 file.open(QIODevice::WriteOnly); //以只写模式打开 Q
  • 序列化Qt数据类型

    2021-04-18 13:25:54
    序列化Qt数据类型序列化Qt数据类型 序列化Qt数据类型 从QDataStream类开始,您可以序列化此部分自版本18开始的Qt数据类型。 在读写时,始终最好将整数转换为Qt整数类型,例如qint16或quint32。这样可以确保您始终...
  • Qt序列化对象

    千次阅读 2016-06-08 15:30:45
    转自:... Qt序列化数据的类是QDataStream,但这个类只能序列化基本类型和一部分Qt定义的类,对于自定义类需要自己编写序列化函数。 在自定义类中声明友元函数,并实现: class CKDrawB

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 11,290
精华内容 4,516
关键字:

qt序列化