英文:
Check only one checkbox at a time (checkbox given inside a delegate)
问题
Currently all checkboxes are checkable . I need to check only one checkbox.
TableView {
id: table
anchors.fill: parent
model: display_model
TableViewColumn {
role: "checkbox";
delegate: CheckBox {
id: delegate_checkbox
onCheckedChanged: {
console.log(model.index)
}
}
}
英文:
Currently all checkboxes are checkable . I need to check only one checkbox.
TableView {
id: table
anchors.fill: parent
model: display_model
TableViewColumn {
role: "checkbox"
delegate: CheckBox {
id: delegate_chekbox
onCheckedChanged: {
console.log(model.index)
}
}
}
答案1
得分: 1
CheckBox 继承自 AbstractButton,因此可以添加到 ButtonGroup 中。如果 ButtonGroup 的 "exclusive" 属性设置为 true(默认值),那么一次只能选中一个按钮。这还可以让您通过 ButtonGroup 组件轻松访问当前选中的按钮等。
TableView {
id: table
anchors.fill: parent
model: 10
delegate: CheckBox {
ButtonGroup.group: group
}
}
ButtonGroup {
id: group
exclusive: true // 默认值为 "true",所以您不需要设置它
}
请注意,这部分内容已经被翻译,不包含代码部分。
英文:
I want to add that the desired behavior can also be achieved with checkboxes.
CheckBox inherits AbstractButton and therefor can be added to a ButtonGroup. If the ButtonGroup has "exclusive" set to true (default) only one button can be checked at a time. This also offers you easy access to the current checked button etc. via the ButtonGroup component.
TableView {
id: table
anchors.fill: parent
model: 10
delegate: CheckBox {
ButtonGroup.group: group
}
}
ButtonGroup {
id: group
exclusive: true //its default "true" so you don't really have to set this
}
答案2
得分: 0
以下是已翻译的内容:
"对于完整性:如@StephenQuan的评论中所提到的,如果您只需要选择一个项目,请使用RadioButtons。以下是一个小示例:
TableView {
id: table
anchors.fill: parent
model: 10
delegate: RadioButton {
}
}"
英文:
For completness: As mentioned in the comments from @StephenQuan, if you just need one selected item, use RadioButtons. Here a small example:
TableView {
id: table
anchors.fill: parent
model: 10
delegate: RadioButton {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论