QML Global Object

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.


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:

Additionally, the 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 Element Attr CharacterData Text
  • nodeName
  • nodeValue
  • nodeType
  • parentNode
  • childNodes
  • firstChild
  • lastChild
  • previousSibling
  • nextSibling
  • attributes
  • xmlVersion
  • xmlEncoding
  • xmlStandalone
  • documentElement
  • tagName
  • name
  • value
  • ownerElement
  • data
  • length
  • isElementContentWhitespace
  • wholeText

The XMLHttpRequest example demonstrates how to use the 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 Databases subdirectory of QDeclarativeEngine::offlineStoragePath(), currently as SQLite databases.

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

  1. import QtQuick 1.0
  3. Rectangle  {
  4.     color: "white"
  5.     width: 200
  6.     height: 100
  8.     Text  {
  9.         text: "?"
  10.         anchors.horizontalCenter: parent.horizontalCenter
  11.         function findGreetings()  {
  12.             var db = openDatabaseSync("QDeclarativeExampleDB", "1.0", "The Example QML SQL!", 1000000);
  14.             db.transaction(
  15.                 function(tx)  {
  16.                     // Create the database if it doesn't already exist
  17.                     tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)');
  19.                     // Add (another) greeting row
  20.                     tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]);
  22.                     // Show all added greetings
  23.                     var rs = tx.executeSql('SELECT * FROM Greeting');
  25.                     var r = ""
  26.                     for(var i = 0; i < rs.rows.length; i++)  {
  27.                         r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\n"
  28.                     }
  29.                     text = r
  30.                 }
  31.             )
  32.         }
  34.         Component.onCompleted: findGreetings()
  35.     }
  36. }

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

The SQL Local Storage example demonstrates the basics of using the Offline Storage API.

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

Returns the database identified by identifier. 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 Value
NameThe name of the database passed to openDatabase()
VersionThe version of the database passed to openDatabase()
DescriptionThe description of the database passed to openDatabase()
EstimatedSizeThe estimated size (in bytes) of the database passed to openDatabase()
DriverCurrently "QSQLITE"

This data can be used by application tools.

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

This method allows you to perform a Scheme Upgrade.

If the current version of db is not 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.


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.


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 statement, binding the list of values to SQL positional parameters ("?").

It returns a results object, with the following properties:

Type Property Value Applicability
introws.lengthThe number of rows in the resultSELECT
varrows.item(i)Function that returns row i of the resultSELECT
introwsAffectedThe number of rows affected by a modificationUPDATE, DELETE
stringinsertIdThe id of the row insertedINSERT

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 Debugging QML for more information.

Notes provided by the Qt Community

No notes