【问题标题】:Unable to reference highlight id of listview qml无法引用 listview qml 的突出显示 id
【发布时间】:2021-02-07 22:31:13
【问题描述】:

我有一个 listview 突出显示组件,我需要在该组件之外使用它的 id(列表视图内部和外部),但无论我尝试什么,我都会得到一个 reference error: Id is not defined. 无法找到解决方法。我在某处读到,由于突出显示的类型是组件,因此不能在它之外使用。但是我真的需要在外面使用这个id。有人可以帮忙吗 我目前拥有的代码的一个小轮廓是

ListView {
    id: listId
    MouseArea {
        anchors.fill: parent
        onClicked: {
            boxId.visible = false
        } //unable to use 'boxId' like this. Getting reference error
    }

    delegate: Rectangle {}

    highlight: Rectangle {
        id: boxId
    }
}

【问题讨论】:

  • 由于范围限制,您无法访问Listview.highlight 组件。它在访问时根本不存在。你不应该那样做,想想declarative。如果您仍想遵循命令式风格,请使用Listview.highlightItem 引用突出显示的项目。

标签: qt listview qml highlight referenceerror


【解决方案1】:

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

您可以尝试这样的方法,只在它存在时访问它:

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

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

    delegate: Rectangle {}

    highlight: Rectangle {
        id: boxId
    }
}

【讨论】:

  • 感谢@Sandro4912 复制我的评论 :-)
  • 我认为为您评论中提到的内容提供适当的代码示例是个好主意。
猜你喜欢
  • 2022-10-16
  • 1970-01-01
  • 2021-04-02
  • 2012-03-13
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2016-05-25
  • 2013-02-04
相关资源
最近更新 更多