QML
{{other uses}}
{{short description|User interface markup language}}
{{primary sources|date=September 2010}}
{{infobox programming language
| name = QML
| paradigm = Multi-paradigm: declarative, reactive, scripting
| year = {{Start date and age|2009}}
| developer = Qt Project
| latest_release_version = 6.8{{cite web |url=https://www.qt.io/blog/qt-6.8-released|title=Qt 6.8 Released}}
| latest release date = {{Release date and age|2024|10|08}}
| influenced_by = XAML,{{cite web|title=Which interface for a modern application?|url=https://www.scriptol.com/ajax/ajax-xul-xaml.php|website=scriptol}} JSON, JavaScript, Qt
| influenced = Qt, Ring{{cite web |url=http://ring-lang.sourceforge.net/doc1.6/introduction.html#ring-and-other-languages |title=The Ring programming language and other languages |author=Ring Team |date=5 December 2017 |work=ring-lang.net |publisher=ring-lang |access-date=5 December 2017 |archive-date=25 December 2018 |archive-url=https://web.archive.org/web/20181225175312/http://ring-lang.sourceforge.net/doc1.6/introduction.html#ring-and-other-languages |url-status=dead }}
| website = {{URL|https://doc.qt.io/qt/qmlapplications.html}}
}}
{{Infobox file format
| name = QML
| icon =
| extension = .qml
| developer = Qt Project
| website = {{URL|https://doc.qt.io/qt/qmlapplications.html}}
| genre = Scripting language
}}
QML (Qt Meta-object Language{{cite web |title=Qt Quick Tutorial |url=https://wiki.qt.io/Qt_Quick_Tutorial |date=August 7, 2020 |access-date=March 9, 2025}}) is a user interface markup language. It is a declarative language (similar to CSS and JSON) for designing user interface–centric applications. Inline JavaScript code handles imperative aspects. It is associated with Qt Quick, the UI creation kit originally developed by Nokia within the Qt framework. Qt Quick is used for mobile applications where touch input, fluid animations and user experience are crucial. QML is also used with Qt3D{{cite web |title=Qt 3D Overview {{!}} Qt 3D 5.13.1 |url=https://doc.qt.io/qt-5/qt3d-overview.html |website=doc.qt.io}} to describe a 3D scene and a "frame graph" rendering methodology. A QML document describes a hierarchical object tree. QML modules{{cite web |title=All QML Types {{!}} Qt 5.13 |url=https://doc.qt.io/qt-5/qmltypes.html |website=doc.qt.io |access-date=September 7, 2019}} shipped with Qt include primitive graphical building blocks (e.g., Rectangle, Image), modeling components (e.g., FolderListModel, XmlListModel), behavioral components (e.g., TapHandler, DragHandler, State, Transition, Animation), and more complex controls (e.g., Button, Slider, Drawer, Menu). These elements can be combined to build components ranging in complexity from simple buttons and sliders, to complete internet-enabled programs.
QML elements can be augmented by standard JavaScript both inline and via included .js files. Elements can also be seamlessly integrated and extended by C++ components using the Qt framework.
QML is the language; its JavaScript runtime is the custom V4 engine,{{cite web|last=Knoll|first=Lars|date=2013-04-15|title=Evolution of the QML engine, part 1|url=http://blog.qt.io/blog/2013/04/15/evolution-of-the-qml-engine-part-1/|access-date=2018-05-11}} since Qt 5.2;{{cite web|title=What's New in Qt 5.2|url=http://doc.qt.io/qt-5/whatsnew52.html|access-date=2018-05-11}} and Qt Quick is the 2D scene graph and the UI framework based on it. These are all part of the Qt Declarative module, while the technology is no longer called Qt Declarative.
QML and JavaScript code can be compiled into native C++ binaries with the Qt Quick Compiler.{{cite web |title=Qt Quick Compiler |url=http://doc.qt.io/QtQuickCompiler/ |access-date=September 7, 2019}} Alternatively there is a QML cache file format{{cite web |title=Deploying QML Applications {{!}} Qt 5.13 |url=https://doc.qt.io/qt-5/qtquick-deployment.html#qml-caching |website=doc.qt.io |access-date=September 7, 2019}} which stores a compiled version of QML dynamically for faster startup the next time it is run.
Adoption
- KDE Plasma 4,{{cite web|title=Development/Tutorials/Plasma4/QML/GettingStarted|url=https://techbase.kde.org/Development/Tutorials/Plasma4/QML/GettingStarted|website=KDE TechBase|publisher=KDE}} KDE Plasma 5 and Plasma 6 through Plasma-framework
- [https://liri.io/ Liri OS]
- Simple Desktop Display Manager
- [https://remarkable.com/ reMarkable tablet device]{{cite web|last1=Dragly|first1=Svenn-Arne|title=Developing for the reMarkable tablet|url=https://dragly.org/2017/12/01/developing-for-the-remarkable/index.html|website=dragly|date=December 2017}}{{cite web|title=QML Demo for the reMarkable Paper Tablet|url=https://github.com/reHackable/reHackable-HelloWorld|website=GitHub|date=9 March 2022}}
- Unity2D{{cite web|title=Ubuntu's Unity Written In Qt/QML For "Unity Next"|url=https://www.phoronix.com/scan.php?page=news_item&px=MTMxNzM|publisher=Michael Larabel}}
- Sailfish OS{{cite web|title=Combining C++ with QML in Sailfish OS applications|url=https://sailfishos.org/develop/tutorials/combining-c-with-qml/}}{{cite web|title=Tutorial - QML Live Coding With Qt QmlLive|url=https://sailfishos.org/wiki/Tutorial_-_QML_Live_Coding_With_Qt_QmlLive}}{{cite web|title=QML to C++ and C++ to QML|url=https://together.jolla.com/question/86050/qml-to-c-and-c-to-qml/|publisher=Jolla}}
- BlackBerry 10{{cite web|title=QML fundamentals|url=http://developer.blackberry.com/native/documentation/dev/qml_fundamentals/index.html|publisher=Blackberry}}
- MeeGo{{cite web|title=Intro to QML for Meego|url=https://slidedocument.com/intro-to-qml-meego|publisher=Nokia|access-date=2018-01-03|archive-date=2018-01-04|archive-url=https://web.archive.org/web/20180104015312/https://slidedocument.com/intro-to-qml-meego|url-status=dead}}{{cite web|title=MeeGo and Qt / QML demos assault MWC|date=23 February 2011 |url=https://www.iotgadgets.com/2011/02/meego-qt-qml-demo-assaults-mwc-nokia-intel/|publisher=IoT Gadgets}}
- Maemo{{cite web|title=QML on N900|url=http://wiki.maemo.org/QML|website=maemo.org|publisher=Maemo Community}}
- Tizen{{cite web|title=Qt Launches on Tizen with Standard Look and Feel|date=20 May 2013|url=http://qtfortizen.blogspot.no/2013/05/1.0alpha1.html}}
- Mer{{cite web|url=http://www.merproject.org/|title=Mer}}{{cite web|url=https://wiki.merproject.org/wiki/Main_Page|title=Mer wiki}}{{cite web|title=Lipstick QML UI on MeeGo CE / Mer|date=6 October 2011 |url=https://www.iotgadgets.com/2011/10/lipstick-qml-ui-meego-ce-mer/|publisher=IoT Gadgets}}
- Ubuntu Phone{{cite web|title=QML - the best tool to unlock your creativity|url=https://docs.ubuntu.com/phone/en/apps/qml/|publisher=Ubuntu}}
- Lumina (desktop environment){{cite web|title=Looking at Lumina Desktop 2.0|date=19 August 2020 |url=https://www.trueos.org/blog/looking-lumina-desktop-2-0/|publisher=TrueOS}}
- Many [https://github.com/search?l=QML&q=qml&type=Repositories open-source applications]
Syntax, semantics
= Basic syntax =
Example:
import QtQuick
Rectangle {
id: canvas
width: 250
height: 200
color: "blue"
Image {
id: logo
source: "pics/logo.png"
anchors.centerIn: parent
x: canvas.height / 5
}
}
Objects are specified by their type, followed by a pair of braces. Object types always begin with a capital letter. In the example above, there are two objects, a Rectangle; and its child, an Image. Between the braces, one can specify information about the object, such as its properties.
Properties are specified as property: value. In the example above, we can see the Image has a property named source, which has been assigned the value pics/logo.png
. The property and its value are separated by a colon.
The id property
Each object can be given a special unique property called an id. Assigning an id enables the object to be referred to by other objects and scripts.
The first Rectangle element below has an id, myRect
. The second Rectangle element defines its own width by referring to myRect.width
, which means it will have the same width value as the first Rectangle element.
Item {
Rectangle {
id: myRect
width: 120
height: 100
}
Rectangle {
width: myRect.width
height: 200
}
}
Note that an id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, digits and underscores.
= Property bindings =
A property binding specifies the value of a property in a declarative way. The property value is automatically updated if the other properties or data values change, following the reactive programming paradigm.
Property bindings are created implicitly in QML whenever a property is assigned a JavaScript expression. The following QML uses two property bindings to connect the size of the rectangle to that of otherItem.
Rectangle {
width: otherItem.width
height: otherItem.height
}
QML extends a standards-compliant JavaScript engine, so any valid JavaScript expression can be used as a property binding. Bindings can access object properties, make function calls, and even use built-in JavaScript objects like Date and Math.
Example:
Rectangle {
function calculateMyHeight() {
return Math.max(otherItem.height, thirdItem.height);
}
anchors.centerIn: parent
width: Math.min(otherItem.width, 10)
height: calculateMyHeight()
color: width > 10 ? "blue" : "red"
}
= States =
States are a mechanism to combine changes to properties in a semantic unit. A button for example has a pressed and a non-pressed state, an address book application could have a read-only and an edit state for contacts. Every element has an "implicit" base state. Every other state is described by listing the properties and values of those elements which differ from the base state.
Example:
In the default state, myRect is positioned at 0,0. In the "moved" state, it is positioned at 50,50. Clicking within the mouse area changes the state from the default state to the "moved" state, thus moving the rectangle.
import QtQuick
Item {
id: myItem
width: 200; height: 200
Rectangle {
id: myRect
width: 100; height: 100
color: "red"
}
states: [
State {
name: "moved"
PropertyChanges {
target: myRect
x: 50
y: 50
}
}
]
MouseArea {
anchors.fill: parent
onClicked: myItem.state = 'moved'
}
}
State changes can be animated using Transitions.
For example, adding this code to the above Item element animates the transition to the "moved" state:
transitions: [
Transition {
from: "*"
to: "moved"
NumberAnimation { properties: "x,y"; duration: 500 }
}
]
= Animation =
Animations in QML are done by animating properties of objects. Properties of type real, int, color, rect, point, size, and vector3d can all be animated.
QML supports three main forms of animation: basic property animation, transitions, and property behaviors.
The simplest form of animation is a PropertyAnimation, which can animate all of the property types listed above.
A property animation can be specified as a value source using the Animation on property syntax. This is especially useful for repeating animations.
The following example creates a bouncing effect:
Rectangle {
id: rect
width: 120; height: 200
Image {
id: img
source: "pics/qt.png"
x: 60 - img.width/2
y: 0
SequentialAnimation on y {
loops: Animation.Infinite
NumberAnimation { to: 200 - img.height; easing.type: Easing.OutBounce; duration: 2000 }
PauseAnimation { duration: 1000 }
NumberAnimation { to: 0; easing.type: Easing.OutQuad; duration: 1000 }
}
}
}
Qt/C++ integration
Usage of QML does not require Qt/C++ knowledge to use, but it can be easily extended via Qt.{{cite web|last1=Alpert|first1=Alan|title=The Qt/QML User Story|url=https://imaginitis.net/the-qtqml-user-story-redux/|website=Incorrigible Imaginings}}{{cite web|last1=Alpert|first1=Alan|title=The many ways to unite QML and C++|url=https://www.qtdeveloperdays.com/north-america/many-ways-unite-qml-and-c.html|website=Qt Developer Days|publisher=BlackBerry}} Any C++ class derived from QObject can be easily registered as a type which can then be instantiated in QML.
= Familiar concepts =
QML provides direct access to the following concepts from Qt:
- QObject signals – can trigger callbacks in JavaScript
- QObject slots – available as functions to call in JavaScript
- QObject properties – available as variables in JavaScript, and for bindings
- QWindow – Window creates a QML scene in a window
- Q*Model – used directly in data binding (e.g. QAbstractItemModel){{cite web|last1=Dahlbom|first1=J|title=QAbstractItemModels in QML views|url=https://jdahlbom.wordpress.com/2010/04/22/qabstractitemmodels-in-qml-views/|website=The missing pieces|date=22 April 2010}}{{cite web|title=Sorting and filtering a TableView|url=https://blog.qt.io/blog/2014/04/16/qt-weekly-6-sorting-and-filtering-a-tableview/|publisher=The Qt Company}}{{cite web|last1=Brad|first1=van der Laan|title=How to use Qt's QSortFilterProxyModel|url=http://imaginativethinking.ca/use-qt-qsortfilterproxymodel/|website=ImaginativeThinking|date=11 July 2014}}
= Signal handlers =
Signal handlers are JavaScript callbacks which allow imperative actions to be taken in response to an event. For instance, the MouseArea element has signal handlers to handle mouse press, release and click:
MouseArea {
onPressed: console.log("mouse button pressed")
}
All signal handler names begin with "on".
Development tools
Because QML and JavaScript are very similar, almost all code editors supporting JavaScript will work. However full support for syntax highlighting, code completion, integrated help, and a WYSIWYG editor are available in the free cross-platform IDE Qt Creator since version 2.1 and many other IDEs.
The qml executable can be used to run a QML file as a script. If the QML file begins with a shebang it can be made directly executable. However packaging an application for deployment (especially on mobile platforms) generally involves writing a simple C++ launcher and packaging the necessary QML files as resources.
References
{{Reflist}}
External links
- [http://doc.qt.io/qt-5/qmlreference.html QML Reference Documentation]
- [http://doc.qt.io/qt-5/qmlfirststeps.html First steps with QML]
- [http://doc.qt.io/qt-5/qtquick-codesamples.html QML Examples and Tutorials]
- [http://blog.qt.io/ Qt Blog]
- [http://doc.qt.io/qt-5/qml-tutorial.html QML Tutorial]
- [http://wiki.qt.io/Developer_Guides Qt Developer Guides]
- [http://blog.qt.io/blog/2010/10/19/exporting-qml-from-photoshop-and-gimp/ Exporting QML from Photoshop and GIMP]
- [https://www.qt.io/product/qt6/qml-book QML Book]
= How-tos =
- [http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html Integrating QML and C++]
- [http://doc.qt.io/qt-5/qtqml-cppclasses-topic.html Important C++ Classes Provided By The Qt QML Module]
{{Qt}}