三态复选框:拒绝用户将其设置为 PartiallyChecked
Tristate checkbox: deny user from setting it to PartiallyChecked
我有三态复选框。我希望能够通过我的代码将其设置为 Qt.PartiallyChecked,但我不希望用户能够将其设置为该状态。
这是我现在使用的解决方法:
CheckBox
{
checkState: allCheckState
tristate: true
onClicked: {
if (checkState == Qt.PartiallyChecked)
checkState = Qt.Checked;
}
}
有更好的解决方案吗?
您可以覆盖 nextCheckState
函数。引用文档:
This property holds a callback function that is called to determine
the next check state whenever the checkbox is interactively toggled by
the user via touch, mouse, or keyboard.
By default, a normal checkbox cycles between Qt.Unchecked and
Qt.Checked states, and a tri-state checkbox cycles between
Qt.Unchecked, Qt.PartiallyChecked, and Qt.Checked states.
The nextCheckState callback function can override the default
behavior. The following example implements a tri-state checkbox that
can present a partially checked state depending on external
conditions, but never cycles to the partially checked state when
interactively toggled by the user.
CheckBox {
tristate: true
checkState: allChildrenChecked ? Qt.Checked :
anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked
nextCheckState: function() {
if (checkState === Qt.Checked)
return Qt.Unchecked
else
return Qt.Checked
}
}
This QML property was introduced in QtQuick.Controls 2.4 (Qt 5.11).
我有三态复选框。我希望能够通过我的代码将其设置为 Qt.PartiallyChecked,但我不希望用户能够将其设置为该状态。
这是我现在使用的解决方法:
CheckBox
{
checkState: allCheckState
tristate: true
onClicked: {
if (checkState == Qt.PartiallyChecked)
checkState = Qt.Checked;
}
}
有更好的解决方案吗?
您可以覆盖 nextCheckState
函数。引用文档:
This property holds a callback function that is called to determine the next check state whenever the checkbox is interactively toggled by the user via touch, mouse, or keyboard.
By default, a normal checkbox cycles between Qt.Unchecked and Qt.Checked states, and a tri-state checkbox cycles between Qt.Unchecked, Qt.PartiallyChecked, and Qt.Checked states.
The nextCheckState callback function can override the default behavior. The following example implements a tri-state checkbox that can present a partially checked state depending on external conditions, but never cycles to the partially checked state when interactively toggled by the user.
CheckBox { tristate: true checkState: allChildrenChecked ? Qt.Checked : anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked nextCheckState: function() { if (checkState === Qt.Checked) return Qt.Unchecked else return Qt.Checked } }
This QML property was introduced in QtQuick.Controls 2.4 (Qt 5.11).