久久久久久久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 | 日韩理论电影在线观看 | 国产成人自拍一区 | 超碰97人人人人人蜜桃 | 欧美日韩在线免费观看 | 日本黄色一级片视频 | 美女三区| xxx.在线观看 | 高清国产午夜精品久久久久久 | 久久免费视频1 | 国产98色在线 | 日韩 | 欧美久久久久 | 国产男人的天堂 | 岛国精品| 日韩一级欧美一级 | 成人不卡视频 | 亚洲天堂影院 | 岛国av一区二区 | 日韩成人免费中文字幕 | 久久网一区二区三区 | 国产女人叫床高潮大片免费 | www.99热| 亚洲精品久久久久中文字幕二区 | 日本一区二区电影 | 99中文字幕 | 欧美一级电影免费 | 激情 一区| www.黄色在线观看 | 可以在线观看av的网站 | 午夜免费视频 | 国产精品日韩在线观看一区二区 | 免费看黄色小视频 | 久久1区| 欧美一区二区久久 | 欧美一级在线 | 亚洲色欲色欲www | 国产一区二区高清在线 | 亚洲看片| 在线中文字幕日韩 | 亚洲 中文 欧美 日韩 在线观看 |