gpt4 book ai didi

qml - 如何将上下文变量分配给 QML 中具有相同名称的属性?

转载 作者:行者123 更新时间:2023-12-02 04:45:18 27 4
gpt4 key购买 nike

这是以下代码的结果:

Line 1234, Line 0, Line 0, Line 0

主.qml

import QtQuick 2.8 

Item {
Reusable {
index: 1234 // reusable with a custom index
}

ListView {
anchors { fill: parent; margins: 20; topMargin: 50 }
model: 3

// Reusable with an index given by the ListView
delegate: Reusable {
index: index // <- does not work, both 'index' point to
// the index property
}
}
}

可重用.qml

import QtQuick 2.8 

Text {
property int index
text: "Line " + index
}

问题描述:

ListView 在每次迭代时将 0、1、2 等分配给变量 index。但是,因为我将它分配给一个属性,所以这个变量被遮蔽了,我无法访问它。

如果我从 Reusable.qml 中删除 property int indexListView 可以工作,但在外部使用 Reusable的 ListView 不再工作。

有没有办法分配index: index

(我可以重命名可用的属性,但我想避免这种情况。)

最佳答案

您可以通过 model 前缀寻址模型相关数据。

ListView {
model: 3

delegate: Reusable { index: model.index }
}

我的建议是即使没有歧义也要这样做,以提高可读性。 IE。阅读代码的开发人员可以立即看到哪些数据是本地属性,哪些数据是由模型提供的。

关于qml - 如何将上下文变量分配给 QML 中具有相同名称的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079578/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com