本页内容

CheckDelegate QML Type

带复选标记的项目委托,该标记可开启或关闭。更多...

Import Statement: import QtQuick.Controls
Inherits:

ItemDelegate

属性

详细说明

CheckDelegate 提供了一种项目委托,可用于切换项目状态(选中或未选中)。CheckDelegate 通常用于从列表中的一组选项中选择一个或多个选项。对于选项较少的情况,或者需要唯一标识选项的情况,建议改用CheckBox 。

CheckDelegate 的 API 继承自ItemDelegate ,而后者又继承自AbstractButton 。例如,您可以设置text ,并使用AbstractButton API 响应clicks 事件。可通过checked 属性设置复选委托的状态。

除了已选中和未选中状态外,还有第三种状态:部分选中。可通过tristate 属性启用部分选中状态。该状态表示无法确定常规的已选中/未选中状态;通常是因为存在其他状态影响了选中委托。例如,当树视图中选中了多个子节点时,此状态就非常有用。

ListView {
    model: ["Option 1", "Option 2", "Option 3"]
    delegate: CheckDelegate {
        text: modelData
    }
}

另请参阅 《自定义 CheckDelegate》、《委托控件》和《CheckBox 》。

属性文档

checkState : enumeration

此属性用于确定检查委托的检查状态。

可用状态:

常量描述
Qt.Unchecked委托未经过检查。
Qt.PartiallyChecked委托处于部分已检查状态。此状态仅在启用“tristate ”时使用。
Qt.Checked委托已通过检查。

另请参阅 tristate 和checked 。

nextCheckState : function [since QtQuick.Controls 2.4 (Qt 5.11)]

该属性保存一个回调函数,当用户通过触摸、鼠标或键盘交互式地切换复选框委托时,系统会调用该函数来确定下一个复选框状态。

默认情况下,普通复选框委托会在Qt.Unchecked 和Qt.Checked 状态之间循环切换,而三态复选框委托则会在Qt.Unchecked 、Qt.PartiallyChecked 和Qt.Checked 状态之间循环切换。

nextCheckState 回调函数可以覆盖默认行为。以下示例实现了一个三态检查委托,它可以根据外部条件呈现部分选中状态,但在用户交互式切换时绝不会进入部分选中状态。

CheckDelegate {
    tristate: true
    checkState: allChildrenChecked ? Qt.Checked :
                   anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked

    nextCheckState: function() {
        if (checkState === Qt.Checked)
            return Qt.Unchecked
        else
            return Qt.Checked
    }
}

该属性首次引入于 QtQuick.Controls 2.4(Qt 5.11)。

tristate : bool

此属性用于确定“检查委托”是否具有三种状态。

在下面的动画中,第一个检查委托具有三种状态:

默认值为false ,即该委托仅具有两种状态。

© 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.