BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Qt5 Tutorial Creating QtQuick2 QML Animation B - 2020





Bookmark and Share





bogotobogo.com site search:




Adding Views

This tutorial is a continuation from the previous tutorial Qt Quick2 QML Animation A.


This was where we left in the previous section:

Transitions_Interim_Result.png

We should see the Earth in the top left rectangle, and two additional rectangles in the center right and bottom left of the screen.

We can now create additional states to add views to the application.





Name of the Panes

Before we move on, let's familiarized ourselves with the names of the pane:

PaneNames.png

  1. Navigator pane displays the items in the current QML file as tree structure.
  2. Library pane displays the building blocks that you can use to design applications: predefined QML types, your own QML components, Qt Quick components or Qt Quick Controls that you import to the project, and other resources.
  3. Canvas is the working area where you create QML components and design applications.
  4. Properties pane organizes the properties of the selected item. You can change the properties also in the code editor.
  5. State pane displays the different states of the item. QML states typically describe user interface configurations, such as the UI controls, their properties and behavior and the available actions. Managing Item Hierarchy.




Adding Views

OK. Let's continue.

In the .qml file, we already created pointers to two additional states: State1 and State2. To create the states:

  1. Click the empty slot in the States pane to create State1.
  2. Click the empty slot in the States pane to create State2.
StatePanes.png

This will create a skeleton of the states in the main.qml:

states: [
    State {
        name: "State1"
    },
    State {
        name: "State2"
    }
]

All we have to do is just to filling it in for the PropertyChanges.

In the code editor, bind the position of the earth icon to the rectangle to make sure that the icon is displayed within the rectangle when the view is scaled on different sizes of screens. Set expressions for the x and y properties, as illustrated by the following code snippet:

 states: [
     State {
         name: "State1"

         PropertyChanges {
             target: icon
             x: middleRightRect.x
             y: middleRightRect.y
         }
     },
     State {
         name: "State2"

         PropertyChanges {
             target: icon
             x: bottomLeftRect.x
             y: bottomLeftRect.y
         }
     }
 ]
AfterStatesSet.png

Press Ctrl+R to run the application.

Click the rectangles to move the earth icon from one rectangle to another:

  1. Press the mouse on the destination rectangle:

    AboutToMove.png

  2. The earth icon moved from base state to State1:

    EarthIconMoved.png





Adding Animation to the View

Add transitions to define how the properties change when the earth icon moves between states. The transitions apply animations to the earth icon. For example, the earth icon bounces back when it moves to the middleRightRect and eases into bottomLeftRect. Add the transitions in the code editor.

In the code editor, add the following highlighted transition code to specify that when moving to State1, the x and y coordinates of the earth icon change linearly over a duration of 1 second:

...
...
    states: [
        State {
            name: "State1"

            PropertyChanges {
                target: icon
                x: middleRightRect.x
                y: middleRightRect.y
            }
        },

        State {
            name: "State2"

            PropertyChanges {
                target: icon
                x: bottomLeftRect.x
                y: bottomLeftRect.y
                fillMode: "Stretch"
            }
        }
    ]

    transitions: [
        Transition {
              from: "*"; to: "State1"
              NumberAnimation {
                  properties: "x,y";
                  duration: 1000
              }
          }
    ]
}

If we run the code now, we can see the earth icon moves in straight line from base state to State1 in one second.

We can use the Qt Quick toolbar for animation to change the easing curve type from linear to OutBounce:

  1. Click NumberAnimation in the code editor to display the icon, and then click the icon to open the toolbar:
    clickThis.png

    LinearTr.png

  2. In the Easing field, select Bounce. Then, nn the Subtype field, select Out.

    OutBounce.png

Add the following code to specify that when moving to State2, the x and y coordinates of the Qt logo change over a duration of 2 seconds, and an InOutQuad easing function is used:

     Transition {
         from: "*"; to: "State2"
         NumberAnimation {
             properties: "x,y";
             easing.type: Easing.InOutQuad;
             duration: 2000
         }
     },

Add the following code to specify that for any other state changes, the x and y coordinates of the Qt logo change linearly over a duration of 200 milliseconds:

     Transition {
         NumberAnimation {
             properties: "x,y";
             duration: 200
         }
     }

Here is the final main.qml:

import QtQuick 2.0

Rectangle {
    id: page
    width: 360
    height: 360
    color: "#343434"

    Image {
        id: icon
        x: 10
        y: 20
        source: "earth.png"
    }

    Rectangle {
        id: topLeftRect
        width: 64
        height: 64
        color: "#00000000"
        radius: 6
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.top: parent.top
        anchors.topMargin: 20
        border.color: "#808080"

        MouseArea {
            anchors.fill: parent
            onClicked: page.state = ''
        }
    }

    Rectangle {
        id: middleRightRect
        x: 6
        y: -6
        width: 64
        height: 64
        color: "#00000000"
        radius: 6
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.verticalCenter: parent.verticalCenter
        border.color: "#808080"
        MouseArea {
            anchors.fill: parent
            onClicked: page.state = 'State1'
        }
    }

    Rectangle {
        id: bottomLeftRect
        x: 4
        y: 0
        width: 64
        height: 64
        color: "#00000000"
        radius: 6
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        border.color: "#808080"
        anchors.left: parent.left
        anchors.leftMargin: 10
        MouseArea {
            anchors.fill: parent
            onClicked: page.state = 'State2'
        }
    }
    states: [
        State {
            name: "State1"

            PropertyChanges {
                target: icon
                x: middleRightRect.x
                y: middleRightRect.y
            }
        },

        State {
            name: "State2"

            PropertyChanges {
                target: icon
                x: bottomLeftRect.x
                y: bottomLeftRect.y
                fillMode: "Stretch"
            }
        }
    ]

    transitions: [
        Transition {
              from: "*"; to: "State1"
              NumberAnimation {
                  easing.type: Easing.OutBounce
                  properties: "x,y";
                  duration: 1000
              }
        },

        Transition {
               from: "*"; to: "State2"
               NumberAnimation {
                   properties: "x,y";
                   easing.type: Easing.InOutQuad;
                   duration: 2000
               }
        },

        Transition {
            NumberAnimation {
                properties: "x,y";
                duration: 200
            }
        }
    ]
}




Run Time

Press Ctrl+R to run the application.

Click the destination rectangles to view the animated transitions.




Screen Shot

FinalRun.png





Animation Recording

Your browser does not support the video tag.

Looks great!





Source Code

Here is the source code:
Transitions.zip.







Qt 5 Tutorial

  1. Hello World
  2. Signals and Slots
  3. Q_OBJECT Macro
  4. MainWindow and Action
  5. MainWindow and ImageViewer using Designer A
  6. MainWindow and ImageViewer using Designer B
  7. Layouts
  8. Layouts without Designer
  9. Grid Layouts
  10. Splitter
  11. QDir
  12. QFile (Basic)
  13. Resource Files (.qrc)
  14. QComboBox
  15. QListWidget
  16. QTreeWidget
  17. QAction and Icon Resources
  18. QStatusBar
  19. QMessageBox
  20. QTimer
  21. QList
  22. QListIterator
  23. QMutableListIterator
  24. QLinkedList
  25. QMap
  26. QHash
  27. QStringList
  28. QTextStream
  29. QMimeType and QMimeDatabase
  30. QFile (Serialization I)
  31. QFile (Serialization II - Class)
  32. Tool Tips in HTML Style and with Resource Images
  33. QPainter
  34. QBrush and QRect
  35. QPainterPath and QPolygon
  36. QPen and Cap Style
  37. QBrush and QGradient
  38. QPainter and Transformations
  39. QGraphicsView and QGraphicsScene
  40. Customizing Items by inheriting QGraphicsItem
  41. QGraphicsView Animation
  42. FFmpeg Converter using QProcess
  43. QProgress Dialog - Modal and Modeless
  44. QVariant and QMetaType
  45. QtXML - Writing to a file
  46. QtXML - QtXML DOM Reading
  47. QThreads - Introduction
  48. QThreads - Creating Threads
  49. Creating QThreads using QtConcurrent
  50. QThreads - Priority
  51. QThreads - QMutex
  52. QThreads - GuiThread
  53. QtConcurrent QProgressDialog with QFutureWatcher
  54. QSemaphores - Producer and Consumer
  55. QThreads - wait()
  56. MVC - ModelView with QListView and QStringListModel
  57. MVC - ModelView with QTreeView and QDirModel
  58. MVC - ModelView with QTreeView and QFileSystemModel
  59. MVC - ModelView with QTableView and QItemDelegate
  60. QHttp - Downloading Files
  61. QNetworkAccessManager and QNetworkRequest - Downloading Files
  62. Qt's Network Download Example - Reconstructed
  63. QNetworkAccessManager - Downloading Files with UI and QProgressDialog
  64. QUdpSocket
  65. QTcpSocket
  66. QTcpSocket with Signals and Slots
  67. QTcpServer - Client and Server
  68. QTcpServer - Loopback Dialog
  69. QTcpServer - Client and Server using MultiThreading
  70. QTcpServer - Client and Server using QThreadPool
  71. Asynchronous QTcpServer - Client and Server using QThreadPool
  72. Qt Quick2 QML Animation - A
  73. Qt Quick2 QML Animation - B
  74. Short note on Ubuntu Install
  75. OpenGL with QT5
  76. Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
  77. Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
  78. Video Player with HTML5 QWebView and FFmpeg Converter
  79. Qt5 Add-in and Visual Studio 2012
  80. Qt5.3 Installation on Ubuntu 14.04
  81. Qt5.5 Installation on Ubuntu 14.04
  82. Short note on deploying to Windows







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Qt 5 Tutorial



Hello World

Signals and Slots

Q_OBJECT Macro

MainWindow and Action

MainWindow and ImageViewer using Designer A

MainWindow and ImageViewer using Designer B

Layouts

Layouts without Designer

Grid Layouts

Splitter

QDir

QFile (Basic)

Resource Files (.qrc)

QComboBox

QListWidget

QTreeWidget

QAction and Icon Resources

QStatusBar

QMessageBox

QTimer

QList

QListIterator

QMutableListIterator

QLinkedList

QMap

QHash

QStringList

QTextStream

QMimeType and QMimeDatabase

QFile (Serialization I)

QFile (Serialization II - Class)

Tool Tips in HTML Style and with Resource Images

QPainter

QBrush and QRect

QPainterPath and QPolygon

QPen and Cap Style

QBrush and QGradient

QPainter and Transformations

QGraphicsView and QGraphicsScene

Customizing Items by inheriting QGraphicsItem

QGraphicsView Animation

FFmpeg Converter using QProcess

QProgress Dialog - Modal and Modeless

QVariant and QMetaType

QtXML - Writing to a file

QtXML - QtXML DOM Reading

QThreads - Introduction

QThreads - Creating Threads

Creating QThreads using QtConcurrent

QThreads - Priority

QThreads - QMutex

QThreads - GuiThread

QtConcurrent QProgressDialog with QFutureWatcher

QSemaphores - Producer and Consumer

QThreads - wait()

MVC - ModelView with QListView and QStringListModel

MVC - ModelView with QTreeView and QDirModel

MVC - ModelView with QTreeView and QFileSystemModel

MVC - ModelView with QTableView and QItemDelegate

QHttp - Downloading Files

QNetworkAccessManager and QNetworkRequest - Downloading Files

Qt's Network Download Example - Reconstructed

QNetworkAccessManager - Downloading Files with UI and QProgressDialog

QUdpSocket

QTcpSocket

QTcpSocket with Signals and Slots

QTcpServer - Client and Server

QTcpServer - Loopback Dialog

QTcpServer - Client and Server using MultiThreading

QTcpServer - Client and Server using QThreadPool

Asynchronous QTcpServer - Client and Server using QThreadPool

Qt Quick2 QML Animation - A

Qt Quick2 QML Animation - B

Short note on Ubuntu Install

OpenGL with QT5

Qt5 Webkit : Web Browser with QtCreator using QWebView Part A

Qt5 Webkit : Web Browser with QtCreator using QWebView Part B

Video Player with HTML5 QWebView and FFmpeg Converter

Qt5 Add-in and Visual Studio 2012

Qt5.3 Installation on Ubuntu 14.04

Qt5.5 Installation on Ubuntu 14.04

Short note on deploying to Windows




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong













Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master