QML
Learn the basics of QML
This is a quick intro to QML. If you are already comfortable with it, skip to the next section.
The official QML tutorial can be found in the QML Documentation.
Item
An Item is a simple visible object. It can have children as well. Item's have a default width and height of 0px, and will not grow to fit their contents. So unlike the HTML box model, you'll need to use layouts mentioned below.
contents/ui/main.qml
Rectangle
If we want to draw a colored rectangle, we can easily do so with Rectangle. For other properties of the Rectangle, like border color and width, read its page in the QML Documentation.
contents/ui/main.qml
Items are 0px wide by default
By default, an Item will not expand to fit its contents. Nor will it expand to fit the width of its parent (like a <div>
in HTML).
In the this example, only the Teal Rectangle will be visible, since the Green Rectangle has the default width of 0px and height of 0px. The Teal Rectangle is only visible since the root item in a widget's main.qml
has a default size which will be explained later.
contents/ui/main.qml
anchors.fill
In this second example, we make the Green Rectangle resize to the parent item, the Teal Rectangle. This will completely cover the Teal Rectangle so only the Green Rectangle will be visible.
contents/ui/main.qml
anchors.bottom
In this third example, we anchor the Green Rectangle to the bottom right, and make it half the width & height of the Teal rectangle. So we end up with a rectangle which is 3/4 teal and 1/4 green.
Other ways to use anchors
properties can be read in the QML Documentation page on Positioning with Anchors and the Item.anchors
property group.
contents/ui/main.qml
ColumnLayout
If you want to stack a number of items on top of each other, you should use a ColumnLayout
.
This example uses Labels which are just fancy Text
items which follow Plasma's color theme. They have a default font size, which means they have their own default height. So they will be stacked on top of each other.
Note that if the ColumnLayout
is taller than its contents, the children will have spacing between them.
contents/ui/main.qml
Layout.fillWidth: true
If you want an item to scale to the parent's width, you have the option of setting it to be the same width as the parent (which doesn't work in a Layout). You can also try anchoring to the left and right (which does work).
Within a Layout however, the proper way to do so is to use the special property attached to the contents of a Layout, Layout.fillWidth
. Setting it to true
will make the item scale to fill up the empty space.
The other Layout related properties can be read here.
contents/ui/main.qml
Layout.fillHeight: true
If you want one item (or several) in a Layout to expand to take up the unused space, you can use Layout.fillHeight: true
.
contents/ui/main.qml
Spacing between items in a Layout
In the last screenshot you might have noticed how there is still spacing between the items. That's because the default ColumnLayout.spacing property is set to 5
. Assigning it to 0
will remove the extra whitespace.
contents/ui/main.qml
Other Layouts
There's also RowLayout and GridLayout. Lastly there's Flow which will treat its contents as if they all had the CSS display: inline-block
.
contents/ui/main.qml
Last updated