bindings inside Repeater to outside properties don't work (?)
-
[https://doc.qt.io/qt-5/qml-qtqml-qt.html](link url) has a non-working example for using Qt.callLater() which purports to show a Column of strings and make them all display with the width of the widest string (the widths of the string do not change as intended).
I tried to figure out why. It has nothing to do with Qt.callLater(), but a seemingly fundamental issue of how bindings work (did that example ever work in older releases?)
Here is a cut-down example of the problem:
import QtQuick 2.12 import QtQuick.Controls 2.12 ApplicationWindow { visible:true Column { id: column property real widestChild: 50 Repeater { id: repeater model: 4 delegate: Rectangle { width: column.widestChild //*** DOES NOT WORK height: 20 color: "lightblue" border.width: 1 } } } }
The binding
width: column.widestChild
seems to have no effect. Nothing is displayed at all, as if width were zero (in the non-working example cited above "widestChild" is computed and set on a onChildrenChanged signal, but in this cut-down example it is a constant).
Why doesn't this work?
-
You can try width: parent.widestChild. It should work.
-
Hi @jimav , i think Qt actually treats "column" as a keyword,so if you replace the id with something else for example:-
Column { id: sampleColumn property real widestChild: 50 [..] [..] delegate: Rectangle { width: sampleColumn.widestChild height: 20 [..] [..] } }
it works. And if you try to print it, it is "undefined"
For Example:-Component.onCompleted: { console.log(column.widestChild) }
And if you take the example given in the [https://doc.qt.io/qt-5/qml-qtqml-qt.html], actually that also does not work, if you try to print "column.widestChild" in that example it comes as "undefined", you can try it by pasting the above code inside the Text like i have done below:-
[..] [..] Repeater { id: repeater model:columnTexts[currentTextModel%3] delegate: Text { color: "white" text: modelData width: column.widestChild horizontalAlignment: Text.Center //####Try this#### Component.onCompleted: { console.log(column.widestChild) } Rectangle { anchors.fill: parent; z: -1; color: index%2 ? "red" : "darkgray" } } } [..] [..]
The only reason the why text or the rectangle is visible to you is because the Text is taking width with respect to the length of strings inside "property var columnTexts:[..]"
-
Ok, it looks like a bug introduced since Qt 5.8. I filed a bug report:
https://bugreports.qt.io/browse/QTBUG-74332