Tutorial QML 3 - Stati e transizioni
In questo capitolo renderemo questo esempio un po' più dinamico introducendo stati e transizioni.
Vogliamo che il nostro testo si sposti nella parte inferiore dello schermo, ruoti e diventi rosso quando viene cliccato.
Ecco il codice QML:
import QtQuick
Rectangle {
id: page
width: 320; height: 480
color: "lightgray"
Text {
id: helloText
text: "Hello world!"
y: 30
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 24; font.bold: true
MouseArea { id: mouseArea; anchors.fill: parent }
states: State {
name: "down"; when: mouseArea.pressed == true
PropertyChanges {
helloText {
y: 160
rotation: 180
color: "red"
}
}
}
transitions: Transition {
from: ""; to: "down"; reversible: true
ParallelAnimation {
NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }
ColorAnimation { duration: 500 }
}
}
}
Grid {
id: colorPicker
x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4
rows: 2; columns: 3; spacing: 3
Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
Cell { cellColor: "black"; onClicked: helloText.color = cellColor }
}
}Procedura dettagliata
states: State {
name: "down"; when: mouseArea.pressed == true
PropertyChanges {
helloText {
y: 160
rotation: 180
color: "red"
}
}
}Per prima cosa, creiamo un nuovo stato "down" per il nostro tipo di testo. Questo stato verrà attivato quando si preme il tasto MouseArea e disattivato quando lo si rilascia.
Lo stato "down" include una serie di modifiche alle proprietà rispetto al nostro stato predefinito implicito (gli elementi così come erano stati inizialmente definiti nel QML). Nello specifico, impostiamo la proprietà " y " del testo su " 160", la rotazione su " 180 " e la proprietà " color " su "red".
transitions: Transition {
from: ""; to: "down"; reversible: true
ParallelAnimation {
NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }
ColorAnimation { duration: 500 }
}
}Poiché non vogliamo che il testo appaia immediatamente in fondo, ma che si sposti in modo fluido, aggiungiamo una transizione tra i nostri due stati.
from e to definiscono gli stati tra i quali verrà eseguita la transizione. In questo caso, vogliamo una transizione dallo stato predefinito al nostro stato "down".
Poiché vogliamo che la stessa transizione venga eseguita al contrario quando si torna dallo stato "down" allo stato predefinito, impostiamo ` reversible ` su ` true`. Ciò equivale a scrivere le due transizioni separatamente.
Il tipo ` ParallelAnimation ` garantisce che i due tipi di animazioni (numero e colore) inizino contemporaneamente. Potremmo anche eseguirle una dopo l’altra utilizzando invece ` SequentialAnimation `.
Per ulteriori dettagli sugli stati e sulle transizioni, consultare la sezione “Stati” inQt Quick e l’esempio sugli stati e le transizioni.
© 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.