无法引用列表视图突出显示中的内容



就像标题所说的那样,我遇到了一个问题,我有一个ListView,通常的delegatehighlight

在我的突出显示中,我放置了一个带有idText组件,以便我可以引用它。

所以行为应该是这样的,我通过ListView的项目移动,当我按下键盘上的数字时,highlight内的文本应该显示它。

但是每当我尝试对上述Text组件执行任何操作时(通过id引用它,例如textComponent.text = "123"我得到一个ReferenceError: textComponent is not defined.

我已经浏览了文档,但没有发现任何与无法通过id访问突出显示的内容有关的内容。

是否有人知道造成这种情况的原因是什么,或者根本不支持这种行为?

我没有包含任何代码,因为这个问题很容易解释和重现,但如果有人需要它,我很乐意包含一小段内容。

编辑代码

ListView
{
height: 500
width: 500
model: ListModel { id: channelListModel }
highlightMoveDuration: 200
highlightRangeMode: ListView.ApplyRange
snapMode: ListView.SnapToItem
preferredHighlightBegin: height * 0.2
preferredHighlightEnd: height * 0.8
delegate: Item
{
id: channelItem
width: ListView.view.width * 0.96
height: parent.height
Text
{
anchors.fill: parent
text: "generic row text"
}
}
Keys.onPressed:
{
switch(event.key)
{
case Qt.Key_0:
case Qt.Key_1:
case Qt.Key_2:
case Qt.Key_3:
case Qt.Key_4:
case Qt.Key_5:
case Qt.Key_6:
case Qt.Key_7:
case Qt.Key_8:
case Qt.Key_9:
textComponent.text = event.key
}
}
highlight: Rectangle
{
id: highlight
color: "#40ffffff"
Text
{
id: textComponent
anchors.fill: parent
}
}

如果您查看ListView的文档,您会发现属性highlight的类型是Component

Component总是为id创建一个新的上下文,就像在另一个文件中一样。 这意味着,您无法从外部访问Component内部id。组件可能已多次实例化或从未实例化 - 因此id不是唯一的。

你能做什么?

ListView中创建property并从组件中读取该。

ListView {
id: myListView
...
property string hightlightText
highlight: SomeItem { // Will be automatically transformed in a Component and initaly not fully created
Text {
text: myListView.highlightText // You can reference ids of the 'outside world'
}
}
}

最新更新