Session 2 - QML Item Placement PDF
Session 2 - QML Item Placement PDF
Session 2: May 1, QML Item Placement Session 6: May 19, Model / View
● How to correctly size and place items ● Model / View
● When to use Anchors, Layouts and ● QML Models
Positioners ● QML Views
The bad:
• There are a lot of binding re-calculations
Text {
id:textLabel
y: 34
text: qsTr("Right-aligned text”)
color: "green"
font { family: "Helvetica"; pixelSize: … }
anchors.right: parent.right
}
anchors.centerIn: anotherItemID
• Sets both the horizontalCenter and verticalCenter
• Can use horizontal and vertical offsets
// SAME AS :
// anchors.horizontalCenter: parent.horizontalCenter
// anchors.verticalCenter: parent.verticalCenter
}
}
● Each item can refer to its parent item with the keyword "parent"
● Can refer to ancestors and named children of ancestors
© 2020 Integrated Computer Solutions Inc. www.ics.com 15
Anchors
Rectangle {
id: rectangle1
width: 400; height: 400
color: "lightblue"
Rectangle {
id: redRectangle
color: "red"
anchors.fill: parent
// SAME AS
// anchors.left : parent.left
// anchors.right : parent.right
// anchors.top : parent.top
// anchors.bottom : parent.bottom
}
}
● Fixed items
○ Make sure these have the id property defined, unless these items can easily be
referenced as parent items
○ Other items should react to the size changes of the dominant item
● Like layouts in Qt
© 2020 Integrated Computer Solutions Inc. www.ics.com 20
Layouts
● Default behavior is similar to positioners
○ GridLayout, RowLayout, ColumnLayout
● However, can be used in the same way as QLayout works for widgets
○ The layout automatically defines the size of the items – no anchors or explicit
width/height needed
● Just set the Layout.fillHeight or Layout.fillWidth to
○ false – if you do not want the layout to use all extra space for the item
○ true – if you want the extra space to be used to expand the item
○ Compare to QSizePolicy::Expanding
● Can specify a preferred width and height with Layout.preferredHeight or
Layout.preferredWidth
GridLayout {
columns: 3
…
Button {
text: qsTr("Btn 2")
Layout.fillHeight: true
… }
Button {
text: qsTr("Btn 4")
Layout.fillWidth: true
… }
}