我想通过主文件中的 ID 访问我的 QML 元素,但它不起作用。在我的测试(案例)环境中,我有 2 个文件:
- main.qml,这是我想通过 ID 访问的文件
- Accounts.qml,这是定义我的元素的文件
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
Item {
Accounts {
id: accounts
}
Component.onCompleted: {
console.log(accounts);
console.log(accounts.accounts_pane);
console.log(accounts.list_area);
console.log(accounts.accounts_pane.list_area.list_column.list_identities.model);
}
}
帐户.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
Pane {
id: accounts_pane
anchors {
fill: parent
}
Rectangle {
id: list_area
border.color: "black"
border.width: 2
anchors {
left: parent.left
right: parent.right
top: parent.top
bottom: parent.bottom
}
Column {
id: list_column
ListView {
id: list_identities
width: list_area.width
height: 200
model: ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
}
delegate: Text {
text: name + ',' + number
}
}
}
}
}
因此,我想从 main.qml 文件更改帐户的模型(不同的公司使用不同的帐户),并且我需要使用 Javascript 来完成此操作。 (我的模型是一个 C++ 类,在 QML 引擎中注册,但我将使用 Javascript 来切换不同的模型对象)。据我了解,访问模型的符号应该是这样的:
accounts.accounts_pane.list_area.list_column.list_identities.model
但是,在测试它时,使用console.log():
console.log(accounts);
console.log(accounts.accounts_pane);
console.log(accounts.list_area);
console.log(accounts.accounts_pane.list_area.list_column.list_identities.model);
我无法传递在 Accounts.qml 中定义的 Pane {} 元素。 qmlscene
的输出向我显示了这些错误:
hier $ qmlscene main.qml
qml: QQuickPane(0x13b85f0)
qml: undefined
qml: undefined
file:///QT_snippets/qmlscenes/hier/main.qml:13: TypeError: Cannot read property 'list_area' of undefined
如何从 main.qml 文档中通过 ID 任意访问 QML 元素?
最佳答案
最好只使用 id 来访问同一 QML 文件中的对象。只要您位于嵌套在具有 id 的组件内的组件范围内,并且它不被任何同名的内容遮蔽,也可以通过 id 访问对象。简而言之,id 只能从内部访问,而不能从外部访问,后者是您的使用场景。
如果您想访问其他 QML 文件中的元素,则必须将它们公开为属性。此外,只要同名属性遮蔽了根 QML 文件中定义的属性,该属性在整个项目中都是可见的。
将 QML 文件中的每个对象视为私有(private)对象,而属性是公共(public)的。如果您想访问这些私有(private)对象之一,请将其公开为属性。
property Item someItem : _item
Item { id: _item }
在您的情况下,accounts_pane
是多余的,因为它将与 accounts
相同 - 它是同一个对象。
如果直接公开list_identities
,则无需公开所有先前的对象即可访问它:
accounts.accounts_pane.list_area.list_column.list_identities.model // no need
accounts.list_identities.model // all you need
此外,您可以将特定对象的特定属性公开为别名:
// in Accounts.qml
property alias model: list_identities.model
// then you can simply
accounts.model
最后,认为这种方法是错误的,您不需要公开所有内容以供外部使用,您应该定义一个接口(interface)来控制每个 QML 组件的内部。
组件的接口(interface)包含它定义的所有顶级属性。
关于Qt:在QML中通过ID任意访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41388939/