Animazioni
Qt Quick offre la possibilità di animare le proprietà. L'animazione delle proprietà consente ai valori delle proprietà di passare attraverso valori intermedi invece di passare immediatamente al valore di destinazione. Per animare la posizione di un elemento, è possibile animare le proprietà che controllano la posizione dell'elemento, ad esempio x e y, in modo che la posizione dell'elemento cambi ad ogni fotogramma lungo il percorso verso la posizione di destinazione.
Interfacce utente fluide
QML è stato progettato per facilitare la creazione di interfacce utente fluide. Si tratta di interfacce utente in cui i componenti dell’interfaccia si animano invece di apparire, scomparire o spostarsi bruscamente. Qt Quick offre due semplici modi per far muovere i componenti dell’interfaccia con un’animazione invece di farli apparire istantaneamente nella loro nuova posizione.
Stati e transizioni
Qt Quick consente di dichiarare vari stati dell’interfaccia utente negli oggetti State. Questi stati sono costituiti da modifiche delle proprietà rispetto a uno stato di base e possono rappresentare un modo utile per organizzare la logica dell’interfaccia utente. Le transizioni sono oggetti che è possibile associare a un elemento per definire come le sue proprietà saranno animate quando cambiano a seguito di un cambiamento di stato.
Gli stati e le transizioni di un elemento possono essere dichiarati tramite le proprietà ` Item::states ` e ` Item::transitions `. Gli stati vengono dichiarati all’interno della proprietà `states list` di un elemento, solitamente l’elemento radice del componente. Le transizioni definite sullo stesso elemento vengono utilizzate per animare i cambiamenti di stato. Ecco un esempio.
Item {
id: container
width: 320
height: 120
Rectangle {
id: rect
color: "red"
width: 120
height: 120
TapHandler {
onTapped: container.state === '' ? container.state = 'other' : container.state = ''
}
}
states: [
// This adds a second state to the container where the rectangle is farther to the right
State { name: "other"
PropertyChanges {
target: rect
x: 200
}
}
]
transitions: [
// This adds a transition that defaults to applying to all state changes
Transition {
// This applies a default NumberAnimation to any changes a state change makes to x or y properties
NumberAnimation { properties: "x,y" }
}
]
}Animazione delle modifiche alle proprietà.
I comportamenti possono essere utilizzati per specificare un'animazione da applicare a una proprietà quando questa cambia. Tale animazione viene quindi applicata a tutte le modifiche, indipendentemente dalla loro origine. L'esempio seguente anima il movimento di un pulsante sullo schermo utilizzando i comportamenti.
Item {
width: 320
height: 120
Rectangle {
color: "green"
width: 120
height: 120
// This is the behavior, and it applies a NumberAnimation to any attempt to set the x property
Behavior on x {
NumberAnimation {
//This specifies how long the animation takes
duration: 600
//This selects an easing curve to interpolate with, the default is Easing.Linear
easing.type: Easing.OutBounce
}
}
TapHandler {
onTapped: parent.x == 0 ? parent.x = 200 : parent.x = 0
}
}
}Altre animazioni
Non tutte le animazioni devono essere legate a una proprietà o a uno stato specifico. È anche possibile creare animazioni in modo più generico e specificare gli elementi di destinazione e le proprietà all’interno dell’animazione. Ecco alcuni esempi di modi diversi per farlo:
Item {
width: 320
height: 120
Rectangle {
color: "blue"
width: 120
height: 120
// By setting this SequentialAnimation on x, it and animations within it will automatically animate
// the x property of this element
SequentialAnimation on x {
id: xAnim
// Animations on properties start running by default
running: false
loops: Animation.Infinite // The animation is set to loop indefinitely
NumberAnimation { from: 0; to: 200; duration: 500; easing.type: Easing.InOutQuad }
NumberAnimation { from: 200; to: 0; duration: 500; easing.type: Easing.InOutQuad }
PauseAnimation { duration: 250 } // This puts a bit of time between the loop
}
TapHandler {
// The animation starts running when you click within the rectangle
onTapped: xAnim.running = true
}
}
}
Item {
width: 320
height: 120
Rectangle {
id: rectangle
color: "yellow"
width: 120
height: 120
TapHandler {
// The animation starts running when you click within the rectangle
onTapped: anim.running = true;
}
}
// This animation specifically targets the Rectangle's properties to animate
SequentialAnimation {
id: anim
// Animations on their own are not running by default
// The default number of loops is one, restart the animation to see it again
NumberAnimation { target: rectangle; property: "x"; from: 0; to: 200; duration: 500 }
NumberAnimation { target: rectangle; property: "x"; from: 200; to: 0; duration: 500 }
}
}
Maggiori informazioni sulle animazioni sono disponibili nella pagina Concetti importanti in Qt Quick - Stati, transizioni e animazioni.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.