This page provides an overview of various ways of retrieving and saving data using Qt.
The QIODevice 类是所有文件和数据存储设备的基类,在 Qt Core 。用于读写数据的所有类均从其继承。
设备范例包括 QFile , QBuffer , QTcpSocket ,和 QProcess . QFile is used for reading and writing text, binary files, and resources ,使用 QIODevice interface. A QFile 可以单独使用,或更方便一起使用与 QTextStream or QDataStream .
The QBuffer 类提供 QIODevice 接口为 QByteArray . QTcpSocket 使开发者能建立 TCP 连接并传输数据流。 QProcess 用于启动外部程序,并对该进程进行读写。
The Qt API provides support for data serialization for several use cases.
The Qt SQL module uses driver plugins to communicate with several database APIs. Qt has drivers for SQLite, MySQL, DB2, Borland InterBase, Oracle, ODBC, and PostgreSQL. It is also possible to develop your own driver if Qt does not provide the driver needed.
Qt 的 SQL 类可分为 3 层:
| 层 | 目的 | 范例类 |
|---|---|---|
|
|
|
With all the SQL drivers, with the exception of SQLite, you can connect to a server that is hosting your database system. If you use the embedded MySQL Server, you do not need a MySQL server in order to use that database system.
有关如何构建 SQL 数据库驱动程序的操作指南,见 SQL 数据库驱动程序 .
The Qt SQLite plugin is very suitable for local storage. SQLite is a relational database management system contained in a small (~350 KiB) C library. In contrast to other database management systems, SQLite is not a separate process that is accessed from the client application, but an integral part of it. SQLite operates on a single file, which must be set as the database name when opening a connection. If the file does not exist, SQLite will try to create it.
SQLite has some restrictions regarding multiple users and multiple transactions. If you are reading or writing on a file from different transactions, your application might freeze until one transaction commits or rolls back.
一旦驱动程序部分被设置,就可访问数据使用类, QSqlQueryModel , QSqlTableModel ,和 QSqlRelationalTableModel . QSqlTableModel and QSqlRelationalTableModel 提供可以被用于 Qt 项视图的可编辑模型。 QSqlTableModel 能读/写访问单表,而 QSqlRelationalTableModel 能读/写访问主表 (而非带外键的表)。
以下页面包含把 SQL 纳入应用程序的有关信息:
Qt 提供用于读取和剖析 XML 流及写操作这些流的 API。以下关键类促进这些行动,通过提供必要基础设施:
以下话题对 Qt XML 支持提供更多洞察:
JSON 是基于文本的数据交换开放标准,易于读取和剖析。它被用于表示简单的数据结构和称为对象的关联数组。它与 JavaScript 相关,但独立于语言表示法形式。
对象可以采取 2 种形式:
|
本地存储 API 提供从 QML 和 JavaScript 访问本地离线 SQL 数据库存储的能力。
These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QDeclarativeEngine::offlineStoragePath() as SQLite databases ( SQL 数据库驱动程序 ).
API 符合 HTML5 Web 数据库 API 的同步 API,W3C 工作草案 2009 年 10 月 29 日 ( HTML5 Web 数据库 API ).
见 Qt Quick 范例 - 本地存储 使用本地存储 API 的基本演示。
The QSettings 类为应用程序设置提供持久存储。通常,应用程序会记住其来自先前会话的设置。
设置在不同平台上的存储,是不同的。例如:在 Windows,它们被存储在注册表中,而在 macOS,它们被存储在 XML 文件中。
QSettings 使您能够以可移植方式保存和还原应用程序设置。构造和销毁 QSettings 对象是轻量、快速的。当创建对象 QSettings ,好的实践是不仅要指定应用程序名称,还指定组织名称。例如:
QSettings settings("MyCompany", "Accountancy");
Qt 资源系统 is a platform-independent mechanism for storing binary files in the application's executable. This is handy if your application frequently needs a certain file, or set of files. It also protects against loss of that particular file.
可以把资源数据编译成二进制并立即在应用程序代码中进行访问,或动态创建二进制资源并通过应用程序采用资源系统进行注册。
默认情况下,资源可以通过如在源树中存储的相同文件名从应用程序代码进行访问,采用
:/
前缀,或通过 URL 采用 qrc 方案。
存档文件是一批文件或目录,通常压缩以缩减在驱动器上消耗的空间。存档文件的范例是 ZIP、TAR、RAR 及 7z。
Qt 支持由 zlib 产生的存档 (见 qCompress() 和 qUncompress() ).