Qt Reference Documentation

- Qt 4.8
- ALL VERSIONS

- A
- A
- A
- Print

# QML Qt Element

The QML global Qt object provides useful enums and functions from Qt. More...

- List of all members, including inherited members

## Properties

- application : object

## Methods

- atob
- btoa
- createComponent
- createQmlObject
- darker
- fontFamilies
- formatDate
- formatDateTime
- formatTime
- hsla
- include
- isQtObject
- lighter
- md5
- openUrlExternally
- point
- quit
- rect
- resolvedUrl
- rgba
- size
- tint
- vector3d

## Detailed Description

The `Qt` object is a global object with utility functions, properties and enums.

It is not instantiable; to use it, call the members of the global `Qt` object directly. For example:

     import QtQuick 1.0

     Text {
         color: Qt.rgba(1, 0, 0, 1)
         text: Qt.md5("hello, world")
     }

## Enums

The Qt object contains the enums available in the Qt Namespace. For example, you can access the Qt::LeftButton and Qt::RightButton enum values as `Qt.LeftButton` and `Qt.RightButton`.

## Types

The Qt object also contains helper functions for creating objects of specific data types. This is primarily useful when setting the properties of an item when the property has one of the following types:

- `color` - use Qt.rgba(), Qt.hsla(), Qt.darker(), Qt.lighter() or Qt.tint()
- `rect` - use Qt.rect()
- `point` - use Qt.point()
- `size` - use Qt.size()
- `vector3d` - use Qt.vector3d()

There are also string based constructors for these types. See QML Basic Types for more information.

## Date/Time Formatters

The Qt object contains several functions for formatting QDateTime, QDate and QTime values.

- string Qt.formatDateTime(datetime date, variant format)
- string Qt.formatDate(datetime date, variant format)
- string Qt.formatTime(datetime date, variant format)

The format specification is described at Qt.formatDateTime.

## Dynamic Object Creation

The following functions on the global object allow you to dynamically create QML items from files or strings. See Dynamic Object Management in QML for an overview of their use.

- object Qt.createComponent(url)
- object Qt.createQmlObject(string qml, object parent, string filepath)

## Property Documentation

read-only application : object

The `application` object provides access to global application state properties shared by many QML components.

Its properties are:

`application.active` | This read-only property indicates whether the application is the top-most and focused application, and the user is able to interact with the application. The property is false when the application is in the background, the device keylock or screen saver is active, the screen backlight is turned off, or the global system dialog is being displayed on top of the application. It can be used for stopping and pausing animations, timers and active processing of data in order to save device battery power and free device memory and processor load when the application is not active.
`application.layoutDirection` | This read-only property can be used to query the default layout direction of the application. On system start-up, the default layout direction depends on the application's language. The property has a value of `Qt.RightToLeft` in locales where text and graphic elements are read from right to left, and `Qt.LeftToRight` where the reading direction flows from left to right. You can bind to this property to customize your application layouts to support both layout directions.

Possible values are:

- Qt.LeftToRight - Text and graphics elements should be positioned from left to right.
- Qt.RightToLeft - Text and graphics elements should be positioned from right to left.

The following example uses the `application` object to indicate whether the application is currently active:

     import QtQuick 1.1

     Rectangle {
         width: 300; height: 55
         color: Qt.application.active ? "white" : "lightgray"
         Text {
             text: "Application " + (Qt.application.active ? "active" : "inactive")
             opacity: Qt.application.active ? 1.0 : 0.5
             anchors.centerIn: parent
         }
     }

This property group was introduced in QtQuick 1.1.

## Method Documentation

string Qt::atob ( data )

ASCII to binary - this function returns a base64 decoding of `data`.

string Qt::btoa ( data )

Binary to ASCII - this function returns a base64 encoding of `data`.

object Qt::createComponent ( url )

Returns a Component object created using the QML file at the specified url, or `null` if an empty string was given.

The returned component's Component::status property indicates whether the component was successfully created. If the status is `Component.Error`, see Component::errorString() for an error description.

Call Component.createObject() on the returned component to create an object instance of the component.

For example:

     import QtQuick 1.0

     Item {
         id: container
         width: 300; height: 300

         function loadButton() {
             var component = Qt.createComponent("Button.qml");
             if (component.status == Component.Ready) {
                 var button = component.createObject(container);
                 button.color = "red";
             }
         }

         Component.onCompleted: loadButton()
     }

See Dynamic Object Management in QML for more information on using this function.

To create a QML object from an arbitrary string of QML (instead of a file), use Qt.createQmlObject().

object Qt::createQmlObject ( string qml, object parent, string filepath )

Returns a new object created from the given string of QML which will have the specified parent, or `null` if there was an error in creating the object.

If filepath is specified, it will be used for error reporting for the created object.

Example (where `parentItem` is the id of an existing QML item):

     var newObject = Qt.createQmlObject('import QtQuick 1.0; Rectangle {color: "red"; width: 20; height: 20}',
         parentItem, "dynamicSnippet1");

In the case of an error, a QtScript Error object is thrown. This object has an additional property, `qmlErrors`, which is an array of the errors encountered. Each object in this array has the members `lineNumber`, `columnNumber`, `fileName` and `message`. For example, if the above snippet had misspelled color as 'colro' then the array would contain an object like the following: { "lineNumber" : 1, "columnNumber" : 32, "fileName" : "dynamicSnippet1", "message" : "Cannot assign to non-existent property "colro""}.

Note that this function returns immediately, and therefore may not work if the qml string loads new components (that is, external QML files that have not yet been loaded). If this is the case, consider using Qt.createComponent() instead.

See Dynamic Object Management in QML for more information on using this function.

color Qt::darker ( color baseColor, real factor )

Returns a color darker than `baseColor` by the `factor` provided.

If the factor is greater than 1.0, this function returns a darker color. Setting factor to 3.0 returns a color that has one-third the brightness. If the factor is less than 1.0, the return color is lighter, but we recommend using the Qt.lighter() function for this purpose. If the factor is 0 or negative, the return value is unspecified.

The function converts the current RGB color to HSV, divides the value (V) component by factor and converts the color back to RGB.

If `factor` is not supplied, returns a color 50% darker than `baseColor` (factor 2.0).

list<string> Qt::fontFamilies ()

Returns a list of the font families available to the application.

string Qt::formatDate ( datetime date, variant format )

Returns a string representation of `date`, optionally formatted according to `format`.

The date parameter may be a JavaScript `Date` object, a date property, a QDate, or QDateTime value. The format parameter may be any of the possible format values as described for Qt.formatDateTime().

If format is not specified, date is formatted using Qt.DefaultLocaleShortDate.

string Qt::formatDateTime ( datetime dateTime, variant format )

Returns a string representation of `datetime`, optionally formatted according to `format`.

The date parameter may be a JavaScript `Date` object, a date property, a QDate, QTime, or QDateTime value.

If format is not provided, dateTime is formatted using Qt.DefaultLocaleShortDate. Otherwise, format should be either.

- One of the Qt::DateFormat enumeration values, such as `Qt.DefaultLocaleShortDate` or `Qt.ISODate`
- A string that specifies the format of the returned string, as detailed below.

If format specifies a format string, it should use the following expressions to specify the date:

Expression | Output
--- | ---
d | the day as number without a leading zero (1 to 31)
dd | the day as number with a leading zero (01 to 31)
ddd | the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
dddd | the long localized day name (e.g. 'Monday' to 'Qt::Sunday'). Uses QDate::longDayName().
M | the month as number without a leading zero (1-12)
MM | the month as number with a leading zero (01-12)
MMM | the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().
MMMM | the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().
yy | the year as two digit number (00-99)
yyyy | the year as four digit number

In addition the following expressions can be used to specify the time:

Expression | Output
--- | ---
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
m | the minute without a leading zero (0 to 59)
mm | the minute with a leading zero (00 to 59)
s | the second without a leading zero (0 to 59)
ss | the second with a leading zero (00 to 59)
z | the milliseconds without leading zeroes (0 to 999)
zzz | the milliseconds with leading zeroes (000 to 999)
AP | use AM/PM display. AP will be replaced by either "AM" or "PM".
ap | use am/pm display. ap will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in single quotes will be treated as text and not be used as an expression. Two consecutive single quotes ("''") are replaced by a single quote in the output.

For example, if the following date/time value was specified:

     // 21 May 2001 14:13:09
     var dateTime = new Date(2001, 5, 21, 14, 13, 09)

This dateTime value could be passed to `Qt.formatDateTime()`, Qt.formatDate() or Qt.formatTime() with the format values below to produce the following results:

Format | Result
--- | ---
"dd.MM.yyyy" | 21.05.2001
"ddd MMMM d yy" | Tue May 21 01
"hh:mm:ss.zzz" | 14:13:09.042
"h:m:s ap" | 2:13:9 pm

string Qt::formatTime ( datetime time, variant format )

Returns a string representation of `time`, optionally formatted according to `format`.

The time parameter may be a JavaScript `Date` object, a QTime, or QDateTime value. The format parameter may be any of the possible format values as described for Qt.formatDateTime().

If format is not specified, time is formatted using Qt.DefaultLocaleShortDate.

color Qt::hsla ( real hue, real saturation, real lightness, real alpha )

Returns a color with the specified `hue`, `saturation`, `lightness` and `alpha` components. All components should be in the range 0-1 inclusive.

object Qt::include ( string url, jsobject callback )

Includes another JavaScript file. This method can only be used from within JavaScript files, and not regular QML files.

This imports all functions from url into the current script's namespace.

Qt.include() returns an object that describes the status of the operation. The object has a single property, `status`, that is set to one of the following values:

Symbol | Value | Description
--- | --- | ---
result.OK | 0 | The include completed successfully.
result.LOADING | 1 | Data is being loaded from the network.
result.NETWORK_ERROR | 2 | A network error occurred while fetching the url.
result.EXCEPTION | 3 | A JavaScript exception occurred while executing the included code. An additional `exception` property will be set in this case.

The `status` property will be updated as the operation progresses.

If provided, callback is invoked when the operation completes. The callback is passed the same object as is returned from the Qt.include() call.

bool Qt::isQtObject ( object )

Returns true if `object` is a valid reference to a Qt or QML object, otherwise false.

color Qt::lighter ( color baseColor, real factor )

Returns a color lighter than `baseColor` by the `factor` provided.

If the factor is greater than 1.0, this functions returns a lighter color. Setting factor to 1.5 returns a color that is 50% brighter. If the factor is less than 1.0, the return color is darker, but we recommend using the Qt.darker() function for this purpose. If the factor is 0 or negative, the return value is unspecified.

The function converts the current RGB color to HSV, multiplies the value (V) component by factor and converts the color back to RGB.

If `factor` is not supplied, returns a color 50% lighter than `baseColor` (factor 1.5).

string Qt::md5 ( data )

Returns a hex string of the md5 hash of `data`.

bool Qt::openUrlExternally ( url target )

Attempts to open the specified `target` url in an external application, based on the user's desktop preferences. Returns true if it succeeds, and false otherwise.

point Qt::point ( int x, int y )

Returns a Point with the specified `x` and `y` coordinates.

Qt::quit ()

This function causes the QDeclarativeEngine::quit() signal to be emitted. Within the QML Viewer, this causes the launcher application to exit; to quit a C++ application when this method is called, connect the QDeclarativeEngine::quit() signal to the QCoreApplication::quit() slot.

rect Qt::rect ( int x, int y, int width, int height )

Returns a `rect` with the top-left corner at `x`, `y` and the specified `width` and `height`.

The returned object has `x`, `y`, `width` and `height` attributes with the given values.

url Qt::resolvedUrl ( url url )

Returns url resolved relative to the URL of the caller.

color Qt::rgba ( real red, real green, real blue, real alpha )

Returns a color with the specified `red`, `green`, `blue` and `alpha` components. All components should be in the range 0-1 inclusive.

Qt::size ( int width, int height )

Returns a Size with the specified `width` and `height`.

color Qt::tint ( color baseColor, color tintColor )

This function allows tinting one color with another.

The tint color should usually be mostly transparent, or you will not be able to see the underlying color. The below example provides a slight red tint by having the tint color be pure red which is only 1/16th opaque.

     Item {
         Rectangle {
             x: 0; width: 80; height: 80
             color: "lightsteelblue"
         }
         Rectangle {
             x: 100; width: 80; height: 80
             color: Qt.tint("lightsteelblue", "#10FF0000")
         }
     }

Tint is most useful when a subtle change is intended to be conveyed due to some event; you can then use tinting to more effectively tune the visible color.

Qt::vector3d ( real x, real y, real z )

Returns a Vector3D with the specified `x`, `y` and `z`.