BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Qt5 Tutorial QFile (Serialization II : class) - 2020





Bookmark and Share





bogotobogo.com site search:




QFile (Serialization II : class)

In this tutorial, we will learn how to serialize a class. This is the continuation of the previous tutorial, QFile (Serialization).



We want to serialize a class which has two members. But unlike the previous example which is serializing primitive variables, this tutorial is dealing with a class. As we may already know, we cannot do cin or cout directly on an object of a class because it does not know how to input/output from/to a class.

Therefore, operators (<< and >>) need be overloaded.

This tutorial is almost the same except those operator overloadings:

Here is the source code, main.cpp:

#include <QCoreApplication>
#include <QFile>
#include <QDataStream>
#include <QString>
#include <QDebug>

class Student
{
public:
    int ID;
    QString Name;

    // ostream, << overloading
    friend QDataStream &Student;::operator<<(QDataStream &out;, const Student &s;)
    {
        out << s.ID << s.Name;
        return out;
    }

    // istream, >> overloading
    friend QDataStream &Student;::operator>>(QDataStream &in;, Student &s;)
    {
        s = Student();
        in >> s.ID >> s.Name;
        return in;
    }
};

void Save()
{
    Student s1;
    s1.ID = 1;
    s1.Name = "Ravel";

    Student s2;
    s2.ID = 2;
    s2.Name = "Schonberg";

    QString filename = "C:/Qt/Test/st.txt";
    QFile file(filename);

    if(!file.open(QIODevice::WriteOnly))
    {
        qDebug() << "Could not open " << filename;
        return;
    }

    QDataStream out(&file;);
    out.setVersion(QDataStream::Qt_5_1);

    out << s1 << s2;

    file.flush();
    file.close();
}

void Load()
{
    Student s1;
    Student s2;
    s2.ID;
    s2.Name;

    QString filename = "C:/Qt/Test/st.txt";
    QFile file(filename);

    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() << "Could not open " << filename;
        return;
    }

    QDataStream in(&file;);
    in.setVersion(QDataStream::Qt_5_1);

    in >> s1 >> s2;

    file.close();

    qDebug() << s1.Name << "'s ID is " << s1.ID;
    qDebug() << s2.Name << "'s ID is " << s2.ID;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    Save();
    Load();

    return a.exec();
}

After we read it back, we get the output like this:

"Ravel" 's ID is  1
"Schonberg" 's ID is  2

Great!

Successful class serialization.



For more on operator overloading, please visit my C++ tutorial, Operator Overloading.












Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Qt 5 Tutorial



Hello World

Signals and Slots

Q_OBJECT Macro

MainWindow and Action

MainWindow and ImageViewer using Designer A

MainWindow and ImageViewer using Designer B

Layouts

Layouts without Designer

Grid Layouts

Splitter

QDir

QFile (Basic)

Resource Files (.qrc)

QComboBox

QListWidget

QTreeWidget

QAction and Icon Resources

QStatusBar

QMessageBox

QTimer

QList

QListIterator

QMutableListIterator

QLinkedList

QMap

QHash

QStringList

QTextStream

QMimeType and QMimeDatabase

QFile (Serialization I)

QFile (Serialization II - Class)

Tool Tips in HTML Style and with Resource Images

QPainter

QBrush and QRect

QPainterPath and QPolygon

QPen and Cap Style

QBrush and QGradient

QPainter and Transformations

QGraphicsView and QGraphicsScene

Customizing Items by inheriting QGraphicsItem

QGraphicsView Animation

FFmpeg Converter using QProcess

QProgress Dialog - Modal and Modeless

QVariant and QMetaType

QtXML - Writing to a file

QtXML - QtXML DOM Reading

QThreads - Introduction

QThreads - Creating Threads

Creating QThreads using QtConcurrent

QThreads - Priority

QThreads - QMutex

QThreads - GuiThread

QtConcurrent QProgressDialog with QFutureWatcher

QSemaphores - Producer and Consumer

QThreads - wait()

MVC - ModelView with QListView and QStringListModel

MVC - ModelView with QTreeView and QDirModel

MVC - ModelView with QTreeView and QFileSystemModel

MVC - ModelView with QTableView and QItemDelegate

QHttp - Downloading Files

QNetworkAccessManager and QNetworkRequest - Downloading Files

Qt's Network Download Example - Reconstructed

QNetworkAccessManager - Downloading Files with UI and QProgressDialog

QUdpSocket

QTcpSocket

QTcpSocket with Signals and Slots

QTcpServer - Client and Server

QTcpServer - Loopback Dialog

QTcpServer - Client and Server using MultiThreading

QTcpServer - Client and Server using QThreadPool

Asynchronous QTcpServer - Client and Server using QThreadPool

Qt Quick2 QML Animation - A

Qt Quick2 QML Animation - B

Short note on Ubuntu Install

OpenGL with QT5

Qt5 Webkit : Web Browser with QtCreator using QWebView Part A

Qt5 Webkit : Web Browser with QtCreator using QWebView Part B

Video Player with HTML5 QWebView and FFmpeg Converter

Qt5 Add-in and Visual Studio 2012

Qt5.3 Installation on Ubuntu 14.04

Qt5.5 Installation on Ubuntu 14.04

Short note on deploying to Windows




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong













Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master