Sprite animation

In this example, we will learn how to create sprite animation in QML.

How to do it…

  1. First of all, we'll need to add our sprite sheet to Qt's resource system so that it can be used in the program. Open up qml.qrc and click the Add | Add Files buttons. Select your sprite sheet image and save the resource file by pressing Ctrl + S.
  2. After that, create a new empty window in main.qml:
    import QtQuick 2.3
    import QtQuick.Window 2.2
    
    Window {
      visible: true
      width: 420
      height: 380
      Rectangle {
        anchors.fill: parent
        color: "white"
      }
    }
  3. Once you're done with that, we will start creating an AnimatedSprite object in QML:
    import QtQuick 2.3 import QtQuick.Window 2.2 Window { visible: true; width: 420; height: 380; Rectangle { anchors.fill: parent; color: ...

Get Qt5 C++ GUI Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.