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() ).