If you prefer to work with QML instead of C++, Qt also provides similar features in Qt Quick that allow you to easily animate a GUI element with the minimum lines of code. In this example, we will learn how to achieve this with QML.
First we will create a new Qt Quick Application project and set up our user interface like so:
Here is what my
main.qml
file looks like:import QtQuick 2.3 import QtQuick.Window 2.2 Window { visible: true width: 480; height: 320; Rectangle { id: background; anchors.fill: parent; color: "blue"; } Text { text: qsTr("Hello World"); anchors.centerIn: parent; color: "white"; font.pointSize: 15; } }
Add the color animation to the
Rectangle
object:Rectangle { id: background; anchors.fill: parent; color: "blue"; SequentialAnimation on color { ColorAnimation { to: "yellow"; duration: 1000 } ColorAnimation { to: "red"; duration: 1000 } ColorAnimation ...