QML
Paradigm | Multi-paradigm: declarative, reactive, scripting |
---|---|
Developer | Qt Project |
First appeared | 2009 (2009) |
Stable release | 5.11.2[1] / September 20, 2018 (2018-09-20) |
Typing discipline | dynamic, strong |
Website | qt-project.org/doc/qt-5/qmlapplications.html |
Influenced by | |
XAML,[2]JSON, JavaScript, Qt | |
Influenced | |
Qt, Ring[3] |
Filename extension | .qml |
---|---|
Developed by | Qt Project |
Type of format | Scripting language |
Website | qt-project.org/doc/qt-5/qmlapplications.html |
QML (Qt Modeling Language[4]) 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 often used for mobile applications where touch input, fluid animations (60 FPS) and user experience are crucial. QML is also used with Qt3D[5] to describe a 3D scene and a "frame graph" rendering methodology. A QML document describes a hierarchical object tree. QML modules[6] 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,[7] since Qt 5.2[8]; 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.[9] Alternatively there is a QML cache file format[10] which stores a compiled version of QML dynamically for faster startup the next time it is run.
Contents
1 Adoption
2 Syntax, semantics
2.1 Basic syntax
2.2 Property bindings
2.3 States
2.4 Animation
3 Qt/C++ integration
3.1 Familiar concepts
3.2 Signal handlers
4 Development tools
5 References
6 External links
6.1 How-tos
Adoption
KDE Plasma 4[11] and KDE Plasma 5 through Plasma-framework
- Liri OS
- Simple Desktop Display Manager
reMarkable tablet device[12][13]
Unity2D[14]
Sailfish OS[15][16][17]
BlackBerry 10[18]
MeeGo[19][20]
Maemo[21]
Tizen[22]
Mer[23][24][25]
Ubuntu Phone[26]
Lumina (desktop environment)[27]
- Many open-source applications
Syntax, semantics
Basic syntax
Example:
import QtQuick 2.9 // import from Qt 5.9
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 2.0
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
QML does not need Qt/C++ knowledge to use and it can be easily extended via Qt.[28][29] 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)[30][31][32]
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
^ "Qt 5.11.2 Released". September 20, 2018..mw-parser-output cite.citation{font-style:inherit}.mw-parser-output q{quotes:"""""""'""'"}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-limited a,.mw-parser-output .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}
^ "Which interface for a modern application?". scriptol.
^ Ring Team (5 December 2017). "The Ring programming language and other languages". ring-lang.net. ring-lang.
^ https://web.archive.org/web/20140325005611/http://blog.qt.digia.com/blog/2009/08/21/qt-declarative-api-changes/#comment-4727
^ https://doc.qt.io/qt-5/qt3d-overview.html
^ https://doc.qt.io/qt-5/qmltypes.html
^ Knoll, Lars (2013-04-15). "Evolution of the QML engine, part 1". Retrieved 2018-05-11.
^ "What's New in Qt 5.2". Retrieved 2018-05-11.
^ http://doc.qt.io/QtQuickCompiler/
^ http://doc.qt.io/qt-5/qtquick-deployment.html#qml-caching
^ "Development/Tutorials/Plasma4/QML/GettingStarted". KDE TechBase. KDE.
^ Dragly, Svenn-Arne. "Developing for the reMarkable tablet". dragly.
^ "QML Demo for the reMarkable Paper Tablet". GitHub.
^ "Ubuntu's Unity Written In Qt/QML For "Unity Next"". Michael Larabel.
^ "Combining C++ with QML in Sailfish OS applications".
^ "Tutorial - QML Live Coding With Qt QmlLive".
^ "QML to C++ and C++ to QML". Jolla.
^ "QML fundamentals". Blackberry.
^ "Intro to QML for Meego". Nokia.
^ "MeeGo and Qt / QML demos assault MWC". IoT Gadgets.
^ "QML on N900". maemo.org. Maemo Community.
^ "Qt Launches on Tizen with Standard Look and Feel".
^ "Mer".
^ "Mer wiki".
^ "Lipstick QML UI on MeeGo CE / Mer". IoT Gadgets.
^ "QML - the best tool to unlock your creativity". Ubuntu.
^ "Looking at Lumina Desktop 2.0". TrueOS.
^ Alpert, Alan. "The Qt/QML User Story". Incorrigible Imaginings.
^ Alpert, Alan. "The many ways to unite QML and C++". Qt Developer Days. BlackBerry.
^ Dahlbom, J. "QAbstractItemModels in QML views". The missing pieces.
^ "Sorting and filtering a TableView". The Qt Company.
^ Brad, van der Laan. "How to use Qt's QSortFilterProxyModel". ImaginativeThinking.
External links
- QML Reference Documentation
- First steps with QML
- QML Examples and Tutorials
- Qt Blog
- QML Tutorial
- Qt Developer Guides
- Exporting QML from Photoshop and GIMP
- Application complete invoicing system in QML - Khitomer
- QML Creator
- QML Book
How-tos
- Integrating QML and C++
- Important C++ Classes Provided By The Qt QML Module