Qt5 Tutorial QFile (Serialization) - 2020
bogotobogo.com site search:
QFile (Serialization)
In this tutorial, we will learn about QFile (Serialization).
So, we'll write (save) a file and then read (load) it back.
Here is the source code:
#include <QCoreApplication> #include <QFile> #include <QDataStream> #include <QString> #include <QMap> #include <QDebug> void Save() { QString filename = "C:/Qt/Test/serial.txt"; QFile file(filename); if(!file.open(QIODevice::WriteOnly)) { qDebug() << "Could not open " << filename; return; } int MyPass = 999; QMapmap; map.insert(1,"uno"); map.insert(2,"dos"); map[3] = "tres"; QDataStream out(&file;); out.setVersion(QDataStream::Qt_5_1); out << MyPass << map; file.flush(); file.close(); } void Load() { QString filename = "C:/Qt/Test/serial.txt"; QFile file(filename); if(!file.open(QIODevice::ReadOnly)) { qDebug() << "Could not open " << filename; return; } int MyPass; QMap map; QDataStream in(&file;); in.setVersion(QDataStream::Qt_5_1); in >> MyPass >> map; file.close(); qDebug() << "MyPass = " << MyPass; foreach(QString item, map.values()) qDebug() << item; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Save(); Load(); return a.exec(); }
We can check what's been written. It's not a plain ASCII but binary file.
After we read it back, we get the output like this:
Great!
Successful serialization.
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
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization