Solved Why can't I define properties called "top", "bottom", "left", and "right"?
-
I'm trying to create a variant on a Rectangle that is positioned by setting its left, right, top, and bottom edge coordinates, rather than its x, width, y, and height. I'm using int properties so that they'll be rounded to integers. So I did this, in a file called RectInt.qml:
import QtQuick 2.11 Rectangle { property int left property int right property int top property int bottom x: left y: top width: right - left height: bottom - top }
I get the following error:
qrc:/RectInt.qml:5 Cannot override FINAL property
If I simply try setting properties of these names on a regular Rectangle, it complains that I'm trying to set read-only properties.
If I change my definition to:
import QtQuick 2.11 Rectangle { property int l property int r property int t property int b x: l y: t width: r - l height: b - t }
then it works.
This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?
-
@pderocco said in Why can't I define properties called "top", "bottom", "left", and "right"?:
This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?
it's not undocumented:
-
@J.Hilk said in Why can't I define properties called "top", "bottom", "left", and "right"?:
@pderocco said in Why can't I define properties called "top", "bottom", "left", and "right"?:
This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?
it's not undocumented:
To clarify @J-Hilk's comment: It is not obvious from the documentation or the QML code, but
left
is linked toanchors.left
.left
,right
, etc. are implemented as "private", read-only properties ofQQuickItem
: https://code.woboq.org/qt5/qtdeclarative/src/quick/items/qquickitem.h.html#122 (Rectangle
inheritsItem
which instantiatesQQuickItem
)This looks like an unfortunate case of leaky implementation details.
-
@J.Hilk
I see now that the anchors documentation mentions those names as properties of "parent", but I wouldn't call that "documentation" of those properties. That's an easy-to-miss reference to undocumented properties.