QML 全局对象

Contains all the properties of the JavaScript global object, plus:

Qt Object

The Qt object provides useful enums and functions from Qt, for use in all QML files.

XMLHttpRequest

QML script supports the XMLHttpRequest object, which can be used to asynchronously obtain data from over a network.

The XMLHttpRequest API implements the same W3C standard as many popular web browsers with following exceptions:

此外, responseXML XML DOM tree currently supported by QML is a reduced subset of the DOM Level 3 Core API supported in a web browser. The following objects and properties are supported by the QML implementation:

Node Document 元素 Attr CharacterData 文本
  • nodeName
  • nodeValue
  • nodeType
  • parentNode
  • childNodes
  • firstChild
  • lastChild
  • previousSibling
  • nextSibling
  • 属性
  • xmlVersion
  • xmlEncoding
  • xmlStandalone
  • documentElement
  • tagName
  • 名称
  • ownerElement
  • data
  • length
  • isElementContentWhitespace
  • wholeText

The XMLHttpRequest example 演示如何使用 XMLHttpRequest object to make a request and read the response headers.

Offline Storage API

Database API

The openDatabaseSync() and related functions provide the ability to access local offline storage in an SQL database.

These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the 数据库 subdirectory of QDeclarativeEngine::offlineStoragePath (), currently as SQLite databases.

The API can be used from JavaScript functions in your QML:


					

The API conforms to the Synchronous API of the HTML5 Web Database API, W3C Working Draft 29 October 2009 .

The SQL 本地存储范例 演示使用离线存储 API 的基础知识。

db = openDatabaseSync(identifier, version, description, estimated_size, callback(db))

Returns the database identified by 标识符 . If the database does not already exist, it is created, and the function callback is called with the database as a parameter. description and estimated_size are written to the INI file (described below), but are otherwise currently unused.

May throw exception with code property SQLException.DATABASE_ERR, or SQLException.VERSION_ERR.

When a database is first created, an INI file is also created specifying its characteristics:

Key
名称 The name of the database passed to openDatabase()
版本 The version of the database passed to openDatabase()
描述 The description of the database passed to openDatabase()
EstimatedSize The estimated size (in bytes) of the database passed to openDatabase()
Driver Currently "QSQLITE"

通过应用程序工具可以使用此数据。

db.changeVersion(from, to, callback(tx))

This method allows you to perform a Scheme Upgrade .

If the current version of db 不是 from , then an exception is thrown.

Otherwise, a database transaction is created and passed to callback . In this function, you can call executeSql on tx to upgrade the database.

May throw exception with code property SQLException.DATABASE_ERR or SQLException.UNKNOWN_ERR.

db.transaction(callback(tx))

This method creates a read/write transaction and passed to callback . In this function, you can call executeSql on tx to read and modify the database.

If the callback throws exceptions, the transaction is rolled back.

db.readTransaction(callback(tx))

This method creates a read-only transaction and passed to callback . In this function, you can call executeSql on tx to read the database (with SELECT statements).

results = tx.executeSql(statement, values)

This method executes a SQL 语句 , binding the list of to SQL positional parameters ("?").

It returns a results object, with the following properties:

类型 特性 Applicability
int rows.length 结果中的行数 SELECT
var rows.item(i) Function that returns row i of the result SELECT
int rowsAffected The number of rows affected by a modification UPDATE, DELETE
string insertId 插入行的 ID INSERT

May throw exception with code property SQLException.DATABASE_ERR, SQLException.SYNTAX_ERR, or SQLException.UNKNOWN_ERR.

日志

console.log() and console.debug() can be used to print information to the console. See 调试 QML 了解更多信息。