diff options
author | Pierre-Yves Siret <gr3cko@gmail.com> | 2020-01-28 23:46:56 +0100 |
---|---|---|
committer | Pierre-Yves Siret <gr3cko@gmail.com> | 2020-01-31 08:43:33 +0100 |
commit | 276d00cff956cb54612ec425b7c40eb50a20d78a (patch) | |
tree | f4b2855b4e1779d36b7d3e3338f1cb196b880c2d /src/quick/util/qquickbehavior.cpp | |
parent | f7647d5adaaed4c651cb2e65c7ce66d7ef6639f1 (diff) |
Add Behavior.targetProperty property
[ChangeLog][Behavior] Behavior now have a targetProperty property to
allow custom logic based on the target property's object or name.
Fixes: QTBUG-70964
Change-Id: I866c10d36c7de181fff48da94c8e16cd73ce49b1
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/quick/util/qquickbehavior.cpp')
-rw-r--r-- | src/quick/util/qquickbehavior.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/quick/util/qquickbehavior.cpp b/src/quick/util/qquickbehavior.cpp index eee8562a2a..ceda5589b2 100644 --- a/src/quick/util/qquickbehavior.cpp +++ b/src/quick/util/qquickbehavior.cpp @@ -185,6 +185,80 @@ QVariant QQuickBehavior::targetValue() const return d->targetValue; } +/*! + \readonly + \qmlpropertygroup QtQuick::Behavior::targetProperty + \qmlproperty string QtQuick::Behavior::targetProperty.name + \qmlproperty Object QtQuick::Behavior::targetProperty.object + + \table + \header + \li Property + \li Description + \row + \li name + \li This property holds the name of the property being controlled by this Behavior. + \row + \li object + \li This property holds the object of the property being controlled by this Behavior. + \endtable + + This property can be used to define custom behaviors based on the name or the object of + the property being controlled. + + The following example defines a Behavior fading out and fading in its target object + when the property it controls changes: + \qml + // FadeBehavior.qml + import QtQuick 2.15 + + Behavior { + id: root + property Item fadeTarget: targetProperty.object + SequentialAnimation { + NumberAnimation { + target: root.fadeTarget + property: "opacity" + to: 0 + easing.type: Easing.InQuad + } + PropertyAction { } // actually change the controlled property between the 2 other animations + NumberAnimation { + target: root.fadeTarget + property: "opacity" + to: 1 + easing.type: Easing.OutQuad + } + } + } + \endqml + + This can be used to animate a text when it changes: + \qml + import QtQuick 2.15 + + Text { + id: root + property int counter + text: counter + FadeBehavior on text {} + Timer { + running: true + repeat: true + interval: 1000 + onTriggered: ++root.counter + } + } + \endqml + + \since QtQuick 2.15 +*/ +QQmlProperty QQuickBehavior::targetProperty() const +{ + Q_D(const QQuickBehavior); + return d->property; +} + void QQuickBehavior::write(const QVariant &value) { Q_D(QQuickBehavior); @@ -265,6 +339,8 @@ void QQuickBehavior::setTarget(const QQmlProperty &property) if (finalizedIdx < 0) finalizedIdx = metaObject()->indexOfSlot("componentFinalized()"); engPriv->registerFinalizeCallback(this, finalizedIdx); + + Q_EMIT targetPropertyChanged(); } void QQuickBehavior::componentFinalized() |