Qt 6.11 is out! See what's new in the release
blog
Assign Behavior{} to multiple Elements
-
In the minimal example below I want to avoid duplicating the Behavior{}.
Is there a way do define a reusable Behavior{} and assign it to myRect and myText?import QtQuick Window { width: 300 height: 120 visible: true Rectangle { id: myRect x: 10 y: 10 width: 100 height: 100 color: rectMouseArea.pressed ? 'lightgreen' : 'gray' Behavior on color { ColorAnimation { duration: 300 } } MouseArea { id: rectMouseArea anchors.fill: parent } } Text { id: myText x: 120 y: 10 font.pixelSize: 50 text: 'foo' color: textMouseArea.pressed ? 'red' : 'blue' // duplicated from myRect :( Behavior on color { ColorAnimation { duration: 300 } } MouseArea { id: textMouseArea anchors.fill: parent } } } -
In the minimal example below I want to avoid duplicating the Behavior{}.
Is there a way do define a reusable Behavior{} and assign it to myRect and myText?import QtQuick Window { width: 300 height: 120 visible: true Rectangle { id: myRect x: 10 y: 10 width: 100 height: 100 color: rectMouseArea.pressed ? 'lightgreen' : 'gray' Behavior on color { ColorAnimation { duration: 300 } } MouseArea { id: rectMouseArea anchors.fill: parent } } Text { id: myText x: 120 y: 10 font.pixelSize: 50 text: 'foo' color: textMouseArea.pressed ? 'red' : 'blue' // duplicated from myRect :( Behavior on color { ColorAnimation { duration: 300 } } MouseArea { id: textMouseArea anchors.fill: parent } } }@PyRookie you could define the Behavior in a new file or an inline component:
// VisibleBehavior.qml Behavior { ColorAnimation { duration: 300 easing.type: Easing.InOutQuad } }Rectangle { // ... color: rectMouseArea.pressed ? 'lightgreen' : 'gray' VisibleBehavior on color {} } -
@PyRookie you could define the Behavior in a new file or an inline component:
// VisibleBehavior.qml Behavior { ColorAnimation { duration: 300 easing.type: Easing.InOutQuad } }Rectangle { // ... color: rectMouseArea.pressed ? 'lightgreen' : 'gray' VisibleBehavior on color {} }