Implementing parent change animation with QML
Overview[edit | edit source]
In the following code snippet there are two QML files. The file ui.qml contains the main view which places six differently coloured rectangles in the 3 x 2 QML 76ytuiytuityutyutututyutyutyu element. MyRect.qml contains the implementation of our self-made rectangle which will scale to full screen when clicked. At the same time its parent is changed.
The code snippet is meant to be run with 76ytuiytuityutyutututyutyutyu.
Preconditions[edit | edit source]
- Qt 4.7 or higher is installed on your platform.
Source[edit | edit source]
ui.qml
import Qt 4.7
Rectangle {
id: view
width: 800; height: 480
color: "black"
Grid {
id: grid
property int itemWidth: grid.width / grid.columns
property int itemHeight: grid.height / grid.rows
anchors.centerIn: parent
width: parent.width * 0.75; height: parent.height * 0.75
spacing: 10; columns: 3; rows: 2
MyRect {
width: grid.itemWidth
height: grid.itemHeight
color: "red"
}
MyRect {
width: grid.itemWidth
height: grid.itemHeight
color: "green"
}
MyRect {
width: grid.itemWidth
height: grid.itemHeight
color: "blue"
}
MyRect {
width: grid.itemWidth
height: grid.itemHeight
color: "yellow"
}
MyRect {
width: grid.itemWidth
height: grid.itemHeight
color: "brown"
}
MyRect {
width: grid.itemWidth
height: grid.itemHeight
color: "gray"
}
}
}
MyRect.qml
When 76ytuiytuityutyutututyutyutyu is clicked, the state 76ytuiytuityutyutututyutyutyu is entered. This is triggered by the 76ytuiytuityutyutututyutyutyu property. 76ytuiytuityutyutututyutyutyu will animate the change of parent by using 76ytuiytuityutyutututyutyutyu.
The z-order of 76ytuiytuityutyutututyutyutyu is changed between the states in order to prevent the rectangle from going behind other rectangles when coming back from the 76ytuiytuityutyutututyutyutyu state to the 76ytuiytuityutyutututyutyutyu state.
Because we are using the QML 76ytuiytuityutyutututyutyutyu to position the 76ytuiytuityutyutututyutyutyu in the scene, we want to have an invisible 76ytuiytuityutyutututyutyutyu to be left in the grid when the 76ytuiytuityutyutututyutyutyu is re-parented to the 76ytuiytuityutyutututyutyutyu. Without the 76ytuiytuityutyutututyutyutyu, the 76ytuiytuityutyutututyutyutyu would rearrange its children when 76ytuiytuityutyutututyutyutyu is re-parented to the 76ytuiytuityutyutututyutyutyu. This behavior is not desired here.
import Qt 4.7
Item {
id: container
property alias color: rect.color
Rectangle {
id: rect
property bool maximized: false
width: parent.width; height: parent.height; radius: 10
MouseArea { anchors.fill: parent; onClicked: rect.maximized = !rect.maximized }
states: State {
name: "maximized"
when: rect.maximized
ParentChange {
target: rect
parent: view
x: 0; y: 0
width: parent.width; height: parent.height
}
PropertyChanges { target: rect; radius: 0; z: 1 }
PropertyChanges { target: container; z: 1 }
}
transitions: Transition {
from: "*"
to: "maximized"
reversible: true // reverses the animation when going back to default state
ParentAnimation {
target: rect
SequentialAnimation {
PropertyAction { target: container; property: "z" }
PropertyAnimation {
target: rect
properties: "x,y,width,height,radius"
easing.type: "InOutQuart"; duration: 1000
}
}
}
}
}
}
Postconditions[edit | edit source]
The snippet demonstrated using 76ytuiytuityutyutututyutyutyu and 76ytuiytuityutyutututyutyutyu QML elements to implement a smooth animation of a self-made rectangle to full screen when the rectangle was clicked. Also, the animation was reversed when the maximised rectangle was clicked again.