QML
Learn the basics of QML
Last updated
Learn the basics of QML
Last updated
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 .
An 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
contents/ui/main.qml
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
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
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.
contents/ui/main.qml
Note that if the ColumnLayout
is taller than its contents, the children will have spacing between them.
contents/ui/main.qml
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).
contents/ui/main.qml
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
contents/ui/main.qml
contents/ui/main.qml
If we want to draw a colored rectangle, we can easily do so with . For other properties of the Rectangle, like border color and width, read its .
By default, an will not expand to fit its contents. Nor will it expand to fit the width of its parent (like a <div>
in HTML).
Other ways to use anchors
properties can be read in the QML Documentation page on and the .
If you want to stack a number of items on top of each other, you should use a .
This example uses Labels which are just fancy 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.
Within a 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 .
In the last screenshot you might have noticed how there is still spacing between the items. That's because the default property is set to 5
. Assigning it to 0
will remove the extra whitespace.
There's also and . Lastly there's which will treat its contents as if they all had the CSS display: inline-block
.