Data Input Output

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 用于启动外部程序,并对该进程进行读写。

Serialized data

The Qt API provides support for data serialization for several use cases.

Database

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 层:

目的 范例类
  • 驱动层
  • SQL API 层
  • 用户界面层
  • 在数据库和 SQL API 层之间的底层通信
  • 提供对数据库的访问
  • 把数据从数据库链接到数据感知小部件

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 支持

Qt 提供用于读取和剖析 XML 流及写操作这些流的 API。以下关键类促进这些行动,通过提供必要基础设施:

  • QDomDocument class represents the entire XML document as a document tree and provides primary access to the document's data.
  • QXmlStreamReader 类提供读取 XML 的剖析器。它是不包括外部剖析实体,格式良好的 XML 1.0 剖析器。
    • It understands and resolves XML namespaces. For example, in case of a StartElement, namespaceUri() returns the namespace the element is in, and name() returns the element's local name. The combination of namespaceUri() and name() uniquely identifies an element.
    • 它不是 CPU 密集型的,因为它并未把整个 XML 文档树存储在内存中。它仅存储当前报告令牌。
  • The QXmlStreamWriter 类提供 XML 写入器采用简单流 API。它搭档 QXmlStreamReader 为写入 XML,且它操作 QIODevice 指定采有 setDevice()。
    • 它是为希望编写的每个 XML 令牌 (或事件) 提供专用函数的简单 API。
    • 它负责基于指定 namespaceUri 为名称空间添加前缀,当写入元素或属性时。若必须使用某些标准化前缀,可以通过采用 writeNamespace() 或 writeDefaultNamespace() 手动声明名称空间以强制作者去使用它们。
    • 它可以通过添加换行和缩进自动格式生成的 XML 数据,以使其可读。可以采用自动格式化特性打开此特征。

以下话题对 Qt XML 支持提供更多洞察:

Qt 中的 JSON

JSON 是基于文本的数据交换开放标准,易于读取和剖析。它被用于表示简单的数据结构和称为对象的关联数组。它与 JavaScript 相关,但独立于语言表示法形式。

对象可以采取 2 种形式:

  • 名称/值对的集合
  • 值的有序列表
  • { "last_name": "Routledge", "first_name": "Ronald", "birth_date": 1960 }
  • "colours": ["green", "blue", "yellow"]

本地存储

本地存储 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 的基本演示。

QSettings 类

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 方案。

Connectivity

文件存档

存档文件是一批文件或目录,通常压缩以缩减在驱动器上消耗的空间。存档文件的范例是 ZIP、TAR、RAR 及 7z。

Qt 支持由 zlib 产生的存档 (见 qCompress() 和 qUncompress() ).