I want to access my QML elements by ID from the main file but it is not working. In my test (case) environment I have 2 files:
- main.qml , this is the file from which I want to access by ID
- Accounts.qml, this is the file where my element is defined
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);
}
}
Accounts.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
}
}
}
}
}
So, from the main.qml file I want to change the model of the Accounts (different Company uses different accounts), and I need to do it with Javascript. (My model is a C++ class, registered within the QML engine but I will be using Javascript to switch different model objects). The notation to access the model, as I understand it, should be something like this:
accounts.accounts_pane.list_area.list_column.list_identities.model
However, when testing it, with 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);
I can't pass past the Pane {} element, which is defined in Accounts.qml. The output of qmlscene shows me these errors:
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
How can you access QML elements by ID arbitrarily from the main.qml document?