Need help! Very weird qml issue - property binding doesn't work! [solved]
-
It's very weird because I use almost the same pattern in another qml file and there it works correctly! I'm sure I'm not reassigning value anywhere!
I have something likeItem { property bool isExpanded: false MouseArea { anchors.fill: parent onClicked: { isExpanded = !isExpanded console.log(isExpanded) console.log(myId.visible) } } MyCustomItem { id: myId visible: isExpanded // other stuff } }
IsExpanded changes after I click but item visibility always stays the same!
And I have many other properties for my item (for example height: isExpanded ? someval : 0)
which doesn't change too! It sort of works If i always change everything manually but whats the point then? And in another .qml I use similar pattern and there it works!
UPDATE: If I put, for example on double click/another button:myId.visible = Qt.binding(function() {return isExpanded})
It works as it should after that! So, for some unknown reason it doesn't 'bind' them when in should in regular property declaration (visible: isExpanded). I had to very explicitly tell qt to bind property to make it work. WTF?
-
I found the issue! Very unobvious (haven't found documented anywhere) and it fails silently.
The problem is that MyCustomItem contained property with the same name in itself (property bool isExpanded) so here:MyCustomItem { id: myId visible: isExpanded // other stuff }
isExpanded was from MyCustomItem. But without any indication of that. Well, now I know that this could happen and would probably spot it if it happened again but this is really counter-intuitive. What if it wasn't my item but someone else's and I didn't know that it had such property? qt creator could've at least warned about possible ambiguities.
-
Hi,
@flashmozzg said:
The problem is that MyCustomItem contained property with the same name in itself
This is called variable shadowing.
this is really counter-intuitive. What if it wasn't my item but someone else's and I didn't know that it had such property?
Idieally, that someone else would document all the properties of the component, and you would read the documentation before using their component.
But to avoid ambiguities, I suggest always specifying the owner whenever you refer to a property that isn't part of the current component, e.g.
MyCustomItem { id: myId visible: parent.isExpanded // other stuff }
qt creator could've at least warned about possible ambiguities.
That could be a useful feature in Debug mode. You can submit a feature request to https://bugreports.qt.io/ if you wish.
I don't want this to be enabled in Release mode though, because it would degrade the performance of the QML engine.