SelectionRectangle QML Type

用于在TableView 中选择表格单元格 ... 更多...

Import Statement: import QtQuick.Controls
Since: Qt 6.2
Inherits:

Control

属性

附属物业

详细说明

SelectionRectangle 用于在TableView 中选择表格单元格。用户可通过在视口中拖动指针或长按单元格顶部来开始选择。

要使 SelectionRectangle 能够选择单元格,TableView 必须分配一个ItemSelectionModelItemSelectionModel 将存储在模型上进行的任何选择,并可用于查询用户选择了哪些单元格。

下面的示例显示了如何使 SelectionRectangle 以TableView 为目标:

    TableView {
        id: tableView
        anchors.fill: parent
        clip: true

        model: TableModel {
            TableModelColumn { display: "name" }
            rows: [ { "name": "Harry" }, { "name": "Hedwig" } ]
        }

        selectionModel: ItemSelectionModel {
            model: tableView.model
        }

        delegate: Rectangle {
            implicitWidth: 100
            implicitHeight: 30
            color: selected ? "blue" : "lightgray"

            required property bool selected

            Text { text: display }
        }
    }

    SelectionRectangle {
        target: tableView
    }

注意: SelectionRectangle 本身不会显示为选择的一部分。只使用委托(如topLeftHandlebottomRightHandle )。您还应考虑rendering the TableView delegate as selected

另请参见 TableView,TableView::selectionModel, 和ItemSelectionModel

属性文档

active : bool [read-only]

当用户执行选择时,该属性为true 。从用户开始选择时起,该选择将处于活动状态,直到选择再次被移除(例如在视口中点击)为止。


bottomRightHandle : Component

该属性包含将显示在选择矩形左上角中心的委托。如果提供了句柄,用户就可以拖动它来调整选区。

删除选区时,默认情况下不会隐藏句柄。相反,这将由委托负责,为自定义淡出动画打开。确保句柄最终隐藏的最简单方法是将visibleSelectionRectangleactive 状态绑定:

SelectionRectangle {
    bottomRightHandle: Rectangle {
        width: 20
        height: 20
        visible: SelectionRectangle.control.active
    }
}

如果不希望在右下角出现选择句柄,请将此属性设置为null

另请参阅 topLeftHandle


dragging : bool [read-only]

每当用户进行指针拖动或手柄拖动以调整矩形选区时,该属性都会被设置为true


selectionMode : enumeration

该属性保留选择开始的时间。

常数说明
SelectionRectangle.Drag在视口中拖动指针时开始选择
SelectionRectangle.PressAndHold按住单元格顶部开始选择
SelectionRectangle.AutoSelectionRectangle 将根据目标和平台选择使用哪种模式。这通常意味着在基于触摸的平台上使用 ,而在桌面平台上使用 。不过, 只有在不与轻触冲突的情况下才会使用。这意味着 需要在 设置为 的情况下配置,或置于 内(默认情况下,鼠标事件的滑动是关闭的),才能选择 。PressAndHold Drag Drag TableView interactive false ScrollView Drag

默认值为Auto


target : Item

该属性包含TableViewSelectionRectangle


topLeftHandle : Component

该属性用于保存将显示在选择矩形左上角中心的委托。如果提供了句柄,用户就可以拖动它来调整选区。

删除选区时,默认情况下不会隐藏句柄。相反,这将由委托负责,为自定义淡出动画打开。确保句柄最终隐藏的最简单方法是将visibleSelectionRectangleactive 状态绑定:

SelectionRectangle {
    topLeftHandle: Rectangle {
        width: 20
        height: 20
        visible: SelectionRectangle.control.active
    }
}

如果不希望在左上方出现选择句柄,请将此属性设置为null

另请参阅 bottomRightHandle


附加属性文档

SelectionRectangle.control : SelectionRectangle

该附加属性包含管理委托实例的SelectionRectangle 。它附加到每个句柄实例。


SelectionRectangle.dragging : bool

如果用户在句柄上拖动,该附加属性将被true 。它附加到每个句柄实例。


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