diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-09-24 11:06:06 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-06-03 14:58:12 +0200 |
commit | 4017505cbcd553f25779a5f449c13863b2c0622c (patch) | |
tree | 6e349e76f1e33b2d1265d7030a28ed5edd5e5f4f /src/quick/items/qquickitem_p.h | |
parent | 2028b74fd24a9e7f822203372fd96af9356e7cf7 (diff) |
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/quick/items/qquickitem_p.h')
-rw-r--r-- | src/quick/items/qquickitem_p.h | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/quick/items/qquickitem_p.h b/src/quick/items/qquickitem_p.h index 99cd3a3269..de6bb4aedd 100644 --- a/src/quick/items/qquickitem_p.h +++ b/src/quick/items/qquickitem_p.h @@ -59,6 +59,7 @@ #include <QtQuick/private/qquickstate_p.h> #include <QtQuick/private/qquickpaletteproviderprivatebase_p.h> #include <QtQuick/private/qquickwindow_p.h> +#include <QtCore/private/qproperty_p.h> #if QT_CONFIG(quick_shadereffect) #include <QtQuick/private/qquickshadereffectsource_p.h> @@ -562,10 +563,18 @@ public: static bool canAcceptTabFocus(QQuickItem *item); - qreal x; - qreal y; - qreal width; - qreal height; + void setX(qreal x) {q_func()->setX(x);} + void xChanged() {q_func()->xChanged();} + Q_OBJECT_COMPAT_PROPERTY(QQuickItemPrivate, qreal, x, &QQuickItemPrivate::setX, &QQuickItemPrivate::xChanged); + void setY(qreal y) {q_func()->setY(y);} + void yChanged() {q_func()->yChanged();} + Q_OBJECT_COMPAT_PROPERTY(QQuickItemPrivate, qreal, y, &QQuickItemPrivate::setY, &QQuickItemPrivate::yChanged); + void setWidth(qreal width) {q_func()->setWidth(width);} + void widthChanged() {q_func()->widthChanged();} + Q_OBJECT_COMPAT_PROPERTY(QQuickItemPrivate, qreal, width, &QQuickItemPrivate::setWidth, &QQuickItemPrivate::widthChanged); + void setHeight(qreal height) {q_func()->setHeight(height);} + void heightChanged() {q_func()->heightChanged();} + Q_OBJECT_COMPAT_PROPERTY(QQuickItemPrivate, qreal, height, &QQuickItemPrivate::setHeight, &QQuickItemPrivate::heightChanged); qreal implicitWidth; qreal implicitHeight; |