
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
The QML global Qt object provides useful enums and functions from Qt.More...
TheQt object is a global object with utility functions, properties and enums.
It is not instantiable; to use it, call the members of the globalQt object directly. For example:
import QtQuick 1.0Text {color:Qt.rgba(1,0,0,1)text:Qt.md5("hello, world")}
The Qt object contains the enums available in theQt Namespace. For example, you can access theQt::LeftButton andQt::RightButton enum values asQt.LeftButton andQt.RightButton.
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. SeeQML Basic Types for more information.
The Qt object contains several functions for formattingQDateTime,QDate andQTime values.
The format specification is described at Qt.formatDateTime.
The following functions on the global object allow you to dynamically create QML items from files or strings. SeeDynamic Object Management in QML for an overview of their use.
objectcreateComponent(url) |
Returns aComponent object created using the QML file at the specifiedurl, ornull if an empty string was given.
The returned component'sComponent::status property indicates whether the component was successfully created. If the status isComponent.Error, seeComponent::errorString() for an error description.
CallComponent.createObject() on the returned component to create an object instance of the component.
For example:
import QtQuick 1.0Item {id:containerwidth:300;height:300functionloadButton() { varcomponent =Qt.createComponent("Button.qml");if (component.status==Component.Ready) { varbutton =component.createObject(container);button.color="red"; } }Component.onCompleted:loadButton()}
SeeDynamic 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().
Returns a new object created from the givenstring of QML which will have the specifiedparent, ornull if there was an error in creating the object.
Iffilepath is specified, it will be used for error reporting for the created object.
Example (whereparentItem is the id of an existing QML item):
varnewObject =Qt.createQmlObject('import QtQuick 1.0; Rectangle {color: "red"; width: 20; height: 20}',parentItem,"dynamicSnippet1");
In the case of an error, aQtScript 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 memberslineNumber,columnNumber,fileName andmessage. 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 theqml 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.
SeeDynamic Object Management in QML for more information on using this function.
Returns a color darker thanbaseColor by thefactor 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.
Iffactor is not supplied, returns a color 50% darker thanbaseColor (factor 2.0).
stringformatDate(datetime date,variant format) |
Returns a string representation ofdate, optionally formatted according toformat.
Thedate parameter may be a JavaScriptDate object, adate property, aQDate, orQDateTime value. Theformat parameter may be any of the possible format values as described for Qt.formatDateTime().
Ifformat is not specified,date is formatted usingQt.DefaultLocaleShortDate.
stringformatDateTime(datetime dateTime,variant format) |
Returns a string representation ofdatetime, optionally formatted according toformat.
Thedate parameter may be a JavaScriptDate object, adate property, aQDate,QTime, orQDateTime value.
Ifformat is not provided,dateTime is formatted usingQt.DefaultLocaleShortDate. Otherwise,format should be either.
Qt.DefaultLocaleShortDate orQt.ISODateIfformat 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'). UsesQDate::shortDayName(). |
| dddd | the long localized day name (e.g. 'Monday' to 'Qt::Sunday'). UsesQDate::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'). UsesQDate::shortMonthName(). |
| MMMM | the long localized month name (e.g. 'January' to 'December'). UsesQDate::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:09var dateTime=new Date(2001,5,21,14,13,09)
ThisdateTime value could be passed toQt.formatDateTime(), Qt.formatDate() or Qt.formatTime() with theformat 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 |
stringformatTime(datetime time,variant format) |
Returns a string representation oftime, optionally formatted according toformat.
Thetime parameter may be a JavaScriptDate object, aQTime, orQDateTime value. Theformat parameter may be any of the possible format values as described for Qt.formatDateTime().
Ifformat is not specified,time is formatted usingQt.DefaultLocaleShortDate.
Returns a color with the specifiedhue,saturation,lightness andalpha components. All components should be in the range 0-1 inclusive.
Includes another JavaScript file. This method can only be used from within JavaScript files, and not regular QML files.
This imports all functions fromurl 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 additionalexception property will be set in this case. |
Thestatus 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.
Returns true ifobject is a valid reference to a Qt or QML object, otherwise false.
Returns a color lighter thanbaseColor by thefactor 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.
Iffactor is not supplied, returns a color 50% lighter thanbaseColor (factor 1.5).
boolopenUrlExternally(url target) |
Attempts to open the specifiedtarget url in an external application, based on the user's desktop preferences. Returns true if it succeeds, and false otherwise.
pointpoint(int x,int y) |
Returns a Point with the specifiedx andy coordinates.
This function causes theQDeclarativeEngine::quit() signal to be emitted. Within theQML Viewer, this causes the launcher application to exit; to quit a C++ application when this method is called, connect theQDeclarativeEngine::quit() signal to theQCoreApplication::quit() slot.
rectrect(int x,int y,int width,int height) |
Returns arect with the top-left corner atx,y and the specifiedwidth andheight.
The returned object hasx,y,width andheight attributes with the given values.
Returns a color with the specifiedred,green,blue andalpha components. All components should be in the range 0-1 inclusive.
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:80color:"lightsteelblue" }Rectangle {x:100;width:80;height:80color: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.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.