Evgenii Legotckoi
Oct. 20, 2015, 10:06 p.m.

QML - Lesson 005. Using a Sprite and AnimatedSprite in QML Qt

Once on the sprites was written three lessons by drawing them to their practical application in Qt in this and this article, I believe that we can not ignore the use of sprites in QML Qt.

Sprites are used in QML is even easier than in Qt / C ++, QML because there are classes that are specifically designed to work with sprites: Sprite and AnimatedSprite.

The project structure to work with Sprite and AnimatedSprite

The project is created as a standard project QtQuick with controls, and then I throw out all superfluous Tipo menyubara and buttons. I am leaving only that we will use for demonstration.

  • main.cpp - the main project file that runs qml file;
  • main.qml - application window;
  • MainForm.ui.qml - form to work with the designer;
  • sprite_sheet.png - the image of our sprite.

sprite_sheet.png

main.qml

As a lesson in itself is quite simple, it will focus on the code only this file.

In this tutorial, I suggest two options for the use of sprites. One embodiment - is the application via SpriteSequence , which can be placed in different sprites with different sources. The second option - is using AnimatedSprite , which is taken one source. Both the use of sprites in this lesson will give the same result.

In SpriteSequence is given sprite size and its position in the main window of the application, while the frame size of the sprite and the number of its personnel is defined already in the Sprite object, which is placed in SpriteSequence . In the SpriteSequence you can put a few of these objects Sprite.

In the case of all parameters AnimatedSprite sizes and locations, as well as its size and frame number are specified in the AnimatedSprite. The difference is that only one image can be taken as a source of sprites.

Also, you must specify frameSync like true. Then sprites will work synchronously, if you do not, then they will run slowly and alternately.

  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3. import QtQuick.Dialogs 1.2
  4.  
  5. ApplicationWindow {
  6. visible: true
  7. width: 640
  8. height: 480
  9. title: qsTr("Hello World")
  10.  
  11. MainForm {
  12. // Stretch the shape of the main window over the application window
  13. anchors.fill: parent
  14.  
  15. /* Stretch the shape of the main window over the application window.
  16.   * Object sprites sequence.
  17.   * This object can be set different sprites with different pictures and their sequence
  18. * */
  19. SpriteSequence {
  20. id: image
  21. width: 20 // The width of the area under the sprite
  22. height: 20 // The height of the area under the sprite
  23. // Sprite is located at the top left of the application window
  24. anchors.left: parent.left
  25. anchors.leftMargin: 100
  26. anchors.top: parent.top
  27. anchors.topMargin: 100
  28.  
  29. Sprite {
  30. id: sprite
  31. source: "sprite_sheet.png"
  32. frameCount: 15
  33. frameWidth: 20
  34. frameHeight: 20
  35. frameSync: true
  36. }
  37. }
  38.  
  39. AnimatedSprite {
  40. id: animatedSprite
  41.  
  42. width: 20 // The width of the area under the sprite
  43.   height: 20 // The height of the area under the sprite
  44. // Sprite is located at the top right of the application window
  45. anchors.right: parent.right
  46. anchors.rightMargin: 100
  47. anchors.top: parent.top
  48. anchors.topMargin: 100
  49.  
  50. source: "sprite_sheet.png"
  51. frameCount: 15
  52. frameWidth: 20
  53. frameHeight: 20
  54. frameSync: true
  55. }
  56. }
  57. }

Conclusion

As a result, you will receive an application in the left and right corner which will house works equally sprites explosions. Demonstration applications, see the video tutorial, are also shown in the video tutorial behaviors sprites if not configured properly.

Video

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup