QML (Qt Quick) is a declarative user interface language that makes it easy to create fun and dynamic applications. Let's dive into QML with this step-by-step tutorial for beginners:
Hello World in QML
To get started, we'll create a simple "Hello World" application in QML:
This QML code defines a Rectangle element as the root object with a size of 200x200. Inside the Rectangle, we have a Text element that displays the text "Hello World!" centered within the parent Rectangle.
import QtQuick 2.0 | |
Rectangle { | |
width: 200; height: 200 | |
Text { | |
text: "Hello World!" | |
font.pointSize: 24 | |
anchors.centerIn: parent | |
} | |
} |
Running QML Files
To run QML files, we need a corresponding .cpp file that imports the QML module and loads the QML source:
#include <QGuiApplication> | |
#include <QQmlApplicationEngine> | |
int main(int argc, char *argv[]) { | |
QGuiApplication app(argc, argv); | |
QQmlApplicationEngine engine; | |
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); | |
return app.exec(); | |
} |
This will load the QML file at "qrc:/main.qml" and display our "Hello World!" text.
Basic Components
QML has many built-in components for creating user interfaces:
Rectangle - A contanier with width, height and color.
Text - Text display component.
Button
Image
Label
ComboBox
ListView
GridView
and many more...
That covers the basics of getting started with QML!