The Problem
Every programming journey starts with a simple program that proves the toolchain works. For QML, that's a window with "Hello, World!" displayed in it. This chapter walks through every character so you understand the structure before we add complexity.
The Naive Approach
If you come from web development, you might expect to write an HTML file with a <script> tag linking to a QML runtime. If you come from systems programming, you might expect to write a main.cpp that creates a QQuickView. Neither is right.
In QML, the source file is the program. You write a .qml file and the runtime executes it directly.
The Idea
A QML file describes a tree of visual objects. The root object is the window itself. Inside it, you place child objects — text, rectangles, images — that form the UI. The QML runtime reads the file, creates the objects, and displays them.
Let's Build It
Create a file named hello.qml:
import QtQuick
import QtQuick.Window
Window {
width: 320
height: 240
visible: true
title: "Hello World"
Text {
text: "Hello, World!"
anchors.centerIn: parent
font.pixelSize: 24
color: "#333333"
}
}Run it with:
qml6 hello.qmlA window appears, 320 by 240 pixels, titled "Hello World," with the text centered inside.
Line by line:
import QtQuick— imports the core Qt Quick module. This gives you access to basic types likeText,Rectangle, andItem.import QtQuick.Window— imports theWindowtype specifically.Window { ... }— the root object. This is the actual OS window.width: 320; height: 240— the window dimensions in pixels.visible: true— windows are invisible by default in QML (you build them up before showing them).title: "Hello World"— the window title bar text.Text { text: "Hello, World!" ... }— a text label inside the window.anchors.centerIn: parent— positions the text at the center of theWindow.font.pixelSize: 24— sets the text size.color: "#333333"— dark gray text color.
Let's Improve It
Let's make it more visually interesting:
import QtQuick
import QtQuick.Window
Window {
width: 400
height: 300
visible: true
title: "Hello World"
Rectangle {
anchors.fill: parent
color: "#f0f0f8"
Text {
text: "Hello, World!"
anchors.centerIn: parent
font.pixelSize: 32
font.bold: true
color: "#1a56db"
}
Text {
text: "Welcome to QML"
anchors {
top: parent.verticalCenter
horizontalCenter: parent.horizontalCenter
topMargin: 40
}
font.pixelSize: 14
color: "#888888"
}
}
}The Rectangle fills the window and provides a background color. Two Text elements are positioned at different vertical offsets.
Forgetting visible: true. The window is created but never shown. If you run your QML file and see no window, check that visible: true is set.
Omitting imports. Without import QtQuick, the Text type is not available. Without import QtQuick.Window, Window is not available. The error message will say "TypeName is not a type."
Under the Hood
When you run qml6 hello.qml, the QML runtime:
- Parses the file into an abstract syntax tree
- Resolves the imports (
QtQuick,QtQuick.Window) - Instantiates the
Windowobject, which creates an OS window via the platform plugin - Sets properties (
width,height,visible,title) - Instantiates the child
Textobject - Evaluates the
anchors.centerIn: parentbinding, positioning the text - Enters the Qt event loop, which handles window system events (paint, resize, input)
The Text element measures the string, selects a font, rasterizes the glyphs, and submits them to the window's scene graph for rendering.
Exercises
- QML files describe a tree of objects, with Window as the root
- import QtQuick and import QtQuick.Window bring the core types into scope
- Properties like width, height, visible, title configure the window
- anchors.centerIn positions an element at its parent center
- qml6 launches a QML file directly without compilation