ポジショナーとレイアウト
QML では、アイテムを配置する方法がいくつかあります。
以下にその概要を簡単に紹介します。詳細については、『 Qt Quick の重要な概念 - 配置』を参照してください。
手動による配置
項目の x、y プロパティを設定することで、画面上の特定の x、y 座標に配置することができます。これにより、視覚座標系の規則に従い、親要素の左上隅を基準とした相対位置が設定されます。
これらのプロパティに定数値の代わりにバインディングを使用することで、x座標とy座標を適切なバインディングに設定するだけで、相対的な配置も簡単に実現できます。
import QtQuick
Item {
width: 100; height: 100
Rectangle {
// Manually positioned at 20,20
x: 20
y: 20
width: 80
height: 80
color: "red"
}
}
アンカー
Item 型は、他のItem 型にアンカーを設定する機能を提供します。 各アイテムには、左、右、垂直中央、上、下、ベースライン、水平中央の 7 つのアンカーラインがあります。3 つの垂直アンカーラインは、他のアイテムの 3 つの垂直アンカーラインのいずれかにアンカーすることができ、4 つの水平アンカーラインは、他のアイテムの水平アンカーラインにアンカーすることができます。
詳細については、「アンカーによる位置指定」およびanchors property のドキュメントを参照してください。
import QtQuick
Item {
width: 200; height: 200
Rectangle {
// Anchored to 20px off the top right corner of the parent
anchors.right: parent.right
anchors.top: parent.top
anchors.margins: 20 // Sets all margins at once
width: 80
height: 80
color: "orange"
}
Rectangle {
// Anchored to 20px off the top center corner of the parent.
// Notice the different group property syntax for 'anchors' compared to
// the previous Rectangle. Both are valid.
anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: 20 }
width: 80
height: 80
color: "green"
}
}
ポジショナー
一連のタイプを規則的なパターンで配置したいという一般的なケースに対応するため、Qt Quick ではいくつかのポジショナー型が用意されています。ポジショナー内に配置されたアイテムは、何らかの方法で自動的に配置されます。たとえば、Row は、アイテムを水平方向に隣接するように(行を形成するように)配置します。
詳細については、「アイテムポジショナー」を参照してください。
import QtQuick
Item {
width: 300; height: 100
Row { // The "Row" type lays out its child items in a horizontal line
spacing: 20 // Places 20px of space between items
Rectangle { width: 80; height: 80; color: "red" }
Rectangle { width: 80; height: 80; color: "green" }
Rectangle { width: 80; height: 80; color: "blue" }
}
}
レイアウトタイプ
レイアウトタイプはポジショナーと同様に機能しますが、レイアウトのさらなる微調整や制限が可能です。具体的には、レイアウトタイプを使用すると以下のことが可能です:
- テキストやその他のアイテムの配置を設定する
- 割り当てられたアプリケーション領域のサイズ変更と自動埋め込み
- 最小値や最大値などのサイズ制限を設定する
- レイアウト内の項目間の間隔を設定する
GroupBox {
id: gridBox
title: "Grid layout"
Layout.fillWidth: true
GridLayout {
id: gridLayout
rows: 3
flow: GridLayout.TopToBottom
anchors.fill: parent
Label { text: "Line 1" }
Label { text: "Line 2" }
Label { text: "Line 3" }
TextField { }
TextField { }
TextField { }
TextArea {
text: "This widget spans over three rows in the GridLayout.\n"
+ "All items in the GridLayout are implicitly positioned from top to bottom."
Layout.rowSpan: 3
Layout.fillHeight: true
Layout.fillWidth: true
}
}
}上記のスニペットは、「Basic Layouts」サンプルからのものです。このスニペットは、レイアウト内にさまざまなフィールドやアイテムを追加する手順がいかに簡単かを示しています。「GridLayout 」のサイズ変更が可能で、そのフォーマットはさまざまなプロパティを通じてカスタマイズできます。
レイアウトの種類に関する詳細については、以下をご覧ください:
- Qt Quick レイアウトの概要
- 「Basic Layouts」のサンプル
注: Qt Quick Layouts はQt 5.1 で導入され、以下の要件が必要です Qt Quick 2.1が必要です。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.