Reputation: 23
My code consistently gives me a ReferenceError
. I have tried to fix this by using
tv.b1Text = " XD "
as in the example, but it didn't work. How can I fix this error? I believe there's a similar problem here.
Here's my code:
Button {
id: b0
text:"b1's text"
onClicked: {
b1.text = " XD "
}
}
TabView {
id: tv
Tab {
id: tab1
grid {
Button{
id: b1
text:"b1's text"
onClicked: {
//console.log(b1.text)
show_text()
}
}
}
}
}
Upvotes: 2
Views: 733
Reputation: 7692
You have a hierarchy in place here and specific scopes. You cannot access id
s without exposing them to higher scopes or without going through the hierarchy, but in the correct way.
Let's examine your TabView
: it has a single Tab
element which contains a Grid
(I'm assuming the grid
element is actually a Grid
!) which in turns contains the Button
you want to modify. If you want to access it from Button
b0
you have to:
tv
item
(via the property of the same name) --> it is Grid
item
of the Grid
(which is the only one, our Button
)Hence, in the current setting, a correct code to modify your b1
text from b0
clicked
signal handler, is the following:
tv.getTab(0).item.children[0].text = " XD "
Note that data[0]
can be used in place of children[0]
(see the Item
element docs). As you can see here you navigate the hierarchy to reach the QML element to be modified.
As you have seen, the previous code is tedious and error prone. A much better approach would be using the aliasing feature as well as Javascript to improve the overall result; other users already kindly advised you about that.
Upvotes: 3