ButtonGroup QML Type
互いに排他的なチェック可能ボタンのグループ。詳細...
| Import Statement: | import QtQuick.Controls |
| Inherits: |
プロパティ
- buttons : list<AbstractButton>
- checkState : enumeration
(since QtQuick.Controls 2.4 (Qt 5.11)) - checkedButton : AbstractButton
- exclusive : bool
(since QtQuick.Controls 2.3 (Qt 5.10))
関連するプロパティ
- group : ButtonGroup
信号
- clicked(AbstractButton button)
(since QtQuick.Controls 2.1 (Qt 5.8))
方法
- void addButton(AbstractButton button)
- void removeButton(AbstractButton button)
詳細な説明
ButtonGroupは、非視覚的で相互排他的なボタンのグループです。RadioButton などのコントロールと組み合わせて使用され、一度に選択できるオプションは1つだけです。
ButtonGroup を使用する最も単純な方法は、ボタンのリストを割り当てることです。例えば、相互排他的なボタンのグループを管理するポジショナーや レイアウトの子要素のリストなどが挙げられます。
ButtonGroup {
buttons: column.children
}
Column {
id: column
RadioButton {
checked: true
text: qsTr("DAB")
}
RadioButton {
text: qsTr("FM")
}
RadioButton {
text: qsTr("AM")
}
}相互に排他的なボタンは、必ずしも同じ親アイテムを共有するとは限りません。また、親レイアウトには、ボタングループに含めるべきではないアイテムが含まれている場合もあります。このような場合は、group のアタッチドプロパティを使用するのが最適です。
ButtonGroup { id: radioGroup }
Column {
Label {
text: qsTr("Radio:")
}
RadioButton {
checked: true
text: qsTr("DAB")
ButtonGroup.group: radioGroup
}
RadioButton {
text: qsTr("FM")
ButtonGroup.group: radioGroup
}
RadioButton {
text: qsTr("AM")
ButtonGroup.group: radioGroup
}
}もう 1 つの方法は、子要素のリストをフィルタリングすることです。これは、リピーターを使用してリストを構成する場合に特に便利です。リピーターも親レイアウトの子要素となるためです。
ButtonGroup {
buttons: column.children.filter((child) => child !== repeater)
}
Column {
id: column
Repeater {
id: repeater
model: [ qsTr("DAB"), qsTr("AM"), qsTr("FM") ]
RadioButton {
required property string modelData
text: modelData
}
}
}より高度なユースケースについては、addButton() およびremoveButton() メソッドを使用して処理できます。
「 RadioButton 」 および「Button Controls」も参照してください 。
プロパティのドキュメント
buttons : list<AbstractButton>
このプロパティには、ボタンのリストが格納されています。
ButtonGroup {
buttons: column.children
}
Column {
id: column
RadioButton {
checked: true
text: qsTr("Option A")
}
RadioButton {
text: qsTr("Option B")
}
}「group」も参照してください 。
checkState : enumeration [since QtQuick.Controls 2.4 (Qt 5.11)]
このプロパティには、ボタングループのチェック状態の合計値が格納されます。
利用可能な状態:
| 定数 | 説明 |
|---|---|
Qt.Unchecked | どのボタンもチェックされていません。 |
Qt.PartiallyChecked | 一部のボタンがチェックされている。 |
Qt.Checked | すべてのボタンがチェックされています。 |
非排他的なボタングループのチェック状態を `Qt.Unchecked ` または `Qt.Checked ` に設定すると、グループ内のすべてのボタンのチェックが外れる、あるいはチェックされる。`Qt.PartiallyChecked ` は無視される。
排他的なボタングループのチェック状態を `Qt.Unchecked ` に設定すると、checkedButton のチェックが解除されます。Qt.Checked およびQt.PartiallyChecked は無視されます。
このプロパティは、QtQuick.Controls 2.4 (Qt 5.11) で導入されました。
checkedButton : AbstractButton
このプロパティは、排他グループ内の現在選択されているボタンを保持します。排他グループが存在しない場合、またはグループが非排他グループである場合は、null を保持します。
デフォルトでは、排他的なボタングループに追加された最初のチェックされたボタンになります。
「exclusive」も参照してください 。
exclusive : bool [since QtQuick.Controls 2.3 (Qt 5.10)]
このプロパティは、ボタングループが排他モードであるかどうかを指定します。デフォルト値は「true 」です。
このプロパティがtrue に設定されている場合、グループ内のボタンはいつでも1つだけチェックされた状態になります。ユーザーは任意のボタンをクリックしてチェックすることができ、そのボタンがグループ内の既存のチェックされたボタンに取って代わります。
排他グループでは、ユーザーは現在チェックされているボタンをクリックしてもそのチェックを解除することはできません。代わりに、グループ内の別のボタンをクリックして、そのグループの新しいチェック対象ボタンを設定する必要があります。
非排他グループでは、ボタンのチェックやチェック解除は、グループ内の他のボタンに影響を与えません。さらに、checkedButton プロパティの値はnull です。
このプロパティは、QtQuick.Controls 2.3 (Qt 5.10) で導入されました。
添付のプロパティのドキュメント
ButtonGroup.group : ButtonGroup [attached]
このプロパティは、ボタングループにボタンを追加します。
ButtonGroup { id: group }
RadioButton {
checked: true
text: qsTr("Option A")
ButtonGroup.group: group
}
RadioButton {
text: qsTr("Option B")
ButtonGroup.group: group
}「buttons」も参照してください 。
シグナルのドキュメント
[since QtQuick.Controls 2.1 (Qt 5.8)] clicked(AbstractButton button)
このシグナルは、グループ内の「button 」がクリックされたときに発火します。
このシグナルは、同じグループ内のすべてのボタンに対して共通のシグナルハンドラを実装するのに便利です。
ButtonGroup {
buttons: column.children
onClicked: button => {
console.log("clicked:", button.text)
}
}
Column {
id: column
Button { text: "First" }
Button { text: "Second" }
Button { text: "Third" }
}注: 対応するハンドラは onClicked です。
このシグナルは、QtQuick.Controls 2.1 (Qt 5.8) で導入されました。
AbstractButton::clicked()も参照してください 。
メソッドのドキュメント
void addButton(AbstractButton button)
ボタングループに「button 」を追加します。
「 buttons 」および「group 」も参照してください 。
void removeButton(AbstractButton button)
ボタングループから「button 」を削除します。
© 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.