无法引用ListView QML的突出显示ID

问题描述

我有一个listview高亮组件,我需要在该组件的外部(在listview的内部和外部)使用其id,但是无论我尝试什么,我都会遇到 SQ City Date Class A Class B Class C 0 1 A 7-1-2020 77 49 68 1 1 A 7-1-2020 61 38 41 2 2 B 7-2-2020 41 36 54 找不到解决方法。我在某处读到,因为突出显示类型是组件,所以不能在其外部使用。但我确实需要在外部使用此ID。谁能帮忙 我目前所拥有的代码的一个简短概述是

reference error: Id is not defined.

解决方法

问题是,当您尝试在MouseArea中访问突出显示的项目时,该项目并不总是存在。因此,不能直接从该范围中引用它。

您可以尝试执行以下操作以仅在存在时对其进行访问:

ListView {
    id: listId
    MouseArea {
        anchors.fill: parent
        onClicked: {
            var item = ListView.highlightItem

            if (item) {
                item.visible = false
            }
        }
    }

    delegate: Rectangle {}

    highlight: Rectangle {
        id: boxId
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...