Tutorial de QML 3: Estados y transiciones
En este capítulo, haremos que este ejemplo sea un poco más dinámico introduciendo estados y transiciones.
Queremos que nuestro texto se desplace hasta la parte inferior de la pantalla, gire y se vuelva rojo al hacer clic en él.
Este es el código 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 }
}
}Guía paso a paso
states: State {
name: "down"; when: mouseArea.pressed == true
PropertyChanges {
helloText {
y: 160
rotation: 180
color: "red"
}
}
}En primer lugar, crearemos un nuevo estado «down» para nuestro tipo de texto. Este estado se activará al pulsar la tecla « MouseArea » y se desactivará al soltarla.
El estado «down» incluye un conjunto de cambios en las propiedades respecto a nuestro estado implícito por defecto (los elementos tal y como se definieron inicialmente en el QML). En concreto, establecemos la propiedad « y » del texto en « 160 », la rotación en « 180 » y la propiedad « color » en rojo.
transitions: Transition {
from: ""; to: "down"; reversible: true
ParallelAnimation {
NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }
ColorAnimation { duration: 500 }
}
}Como no queremos que el texto aparezca en la parte inferior al instante, sino que se desplace de forma fluida, añadimos una transición entre nuestros dos estados.
from y ` to ` definen los estados entre los que se ejecutará la transición. En este caso, queremos una transición del estado predeterminado a nuestro estado «abajo ».
Como queremos que la misma transición se ejecute a la inversa al volver del estado «abajo» al estado predeterminado, establecemos reversible en true. Esto equivale a escribir las dos transiciones por separado.
El tipo « ParallelAnimation » garantiza que los dos tipos de animaciones (número y color) comiencen al mismo tiempo. También podríamos ejecutarlas una tras otra utilizando « SequentialAnimation » en su lugar.
Para obtener más detalles sobre los estados y las transiciones, consulta «Estados deQt Quick » y el ejemplo de estados y transiciones.
© 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.