久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Qt 進行序列化

Serialization with Qt(使用 Qt 進行序列化)
本文介紹了使用 Qt 進行序列化的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 Qt 庫編寫 GUI.在我的 GUI 中,我有一個巨大的 std::map.

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

MyType"是一個具有不同類型字段的類.

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

我想序列化 std::map.我怎樣才能做到這一點?Qt 是否為我們提供了必要的功能?

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

推薦答案

QDataStream 處理各種 C++ 和 Qt 數據類型.完整列表可在 http://doc.qt.io/qt-4.8/datastreamformat.html.我們還可以通過重載 << 來添加對我們自己的自定義類型的支持.和 >> 運算符.以下是可與 QDataStream 一起使用的自定義數據類型的定義:

QDataStream handles a variety of C++ and Qt data types. The complete list is available at http://doc.qt.io/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 &out, const Painting &painting);
QDataStream &operator>>(QDataStream &in, Painting &painting);

以下是我們如何實現 <<操作員:

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

QDataStream &operator<<(QDataStream &out, const Painting &painting)
{
    out << painting.title() << painting.artist()
        << quint32(painting.year());
    return out;
}

要輸出一幅畫,我們只需輸出兩個 QString 和一個 quint32.在函數結束時,我們返回流.這是一個常見的 C++ 習慣用法,它允許我們使用 <<具有輸出流的運算符.例如:

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:

出<<繪畫1<<繪畫2<<繪畫3;

out << painting1 << painting2 << painting3;

operator>>()的實現與operator<<()類似:

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;
}

本文來自:C++ GUI Programming with Qt 4 By Jasmin Blanchette, Mark Summerfield

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

這篇關于使用 Qt 進行序列化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产一级片一区二区三区 | 亚洲福利网 | 欧美精品久久 | 久久久精彩视频 | 日韩 欧美 综合 | 精品亚洲一区二区三区四区五区 | 人人九九精 | 日韩高清一区 | 精品国产欧美一区二区三区成人 | 亚洲 欧美 日韩 在线 | 中文字幕国产一区 | 伊人精品国产 | 一区二区在线观看av | 亚洲精品黑人 | 欧美一区视频 | www.操.com| 91欧美激情一区二区三区成人 | 久久久久国产一区二区三区 | 国产高潮好爽受不了了夜色 | 麻豆精品一区二区三区在线观看 | 岛国av在线免费观看 | 中文字幕日韩在线 | 精品国产一区久久 | 日韩高清在线 | 瑟瑟视频在线看 | 国产激情视频在线 | 日日碰碰 | 亚洲成人一二区 | 久久久久久久国产精品 | 亚洲最新在线视频 | 91国产在线播放 | 国产高清在线精品一区二区三区 | 日本二区在线观看 | 在线一区视频 | 久久精品亚洲 | 欧美日韩在线视频一区二区 | 人人干人人干人人 | 九七午夜剧场福利写真 | 雨宫琴音一区二区在线 | 在线国产一区二区 | 久久麻豆精品 |