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 A - 2020





Bookmark and Share





bogotobogo.com site search:




Introduction

In this tutorial, we will learn how to create a Qt Quick2 application.

We're going to use Qt5.1.1 with Qt Creator2.8.1:


QtCreater281.png



Animation Recording

Your browser does not support the video tag.


Creating the Project

Select File > New File or Project > Applications > Qt Quick2 Application > Choose.

qml_new_project.png

Create a project called Transitions.

Qt Creator generates a default QML file that we can modify to create the main view of the application.

main_qml.png

Press Ctrl+R to run the application.

HelloWorldTransition.png


Creating Main View

The main view of the application displays a earth icon in the top left corner of the screen and two empty rectangles.

To use the earth.png image in our application, we must copy it to the project directory (same subdirectory as the QML file). The image appears in the Resources pane. We can also use any other image or a QML element, instead.

In the Projects view, double-click the main.qml file to open it in the code editor.

Click Design to open the file in Qt Quick Designer:

Designer.png

In the Navigator pane, select Text and press Delete to delete it.

Select Rectangle to edit its properties.

  1. In the Id field, enter page, to be able to reference the rectangle from other places:
  2. In the Colors group, Rectangle field, set the color to #343434:
color343434.png

In the Library view, Resources tab, select earth.png and drag and drop it to the canvas.

  1. While we're drag/drop the icon, we need to make the icon as a child of page. We can tell the relationship by watching navigator while we're moving icon around. If not set properly, we can directly work within the Navigator pane.
  2. In the Id field, enter icon.
  3. In the Position field, set X to 10 and Y to 20.
earth_png_drag_drop.png

In the Library view, QML Types, select Rectangle, drag and drop it to the canvas, and edit its properties.

  1. In the Id field, enter topLeftRect.
  2. In the Size field, set W and H to 64, for the rectangle size to match the image size.
  3. In the Colors group, Rectangle field, click the button to make the rectangle transparent .
  4. In the Border field, set the border color to #808080.
  5. In the Rectangle group, Border field, set the border width to 1.
  6. Note: If the Border field does not appear after you set the border color, try setting the border color to solid by clicking the button.
  7. In the Radius field, select 6 to create rounded corners for the rectangle.
topLeftRect.png

  1. Click Layout, and then click the top and left anchor buttons to anchor the rectangle to the top left corner of the page.
  2. In the Margin field, select 20 for the top anchor and 10 for the left anchor.

  3. Anchor_Margin.png

In the Navigator pane, drag and drop the Mouse Area element from page to topLeftRect to make it apply only to the rectangle and not to the whole page.

Edit Mouse Area properties:

  1. Click Layout, and then click the button to anchor the mouse area to the rectangle.
  2. MouseArea.png

  3. In the code editor, edit the pointer to the clicked expression in the mouse area element, as illustrated by the following code snippet:
    from:
    MouseArea {
          anchors.fill: parent
          onClicked: {
              Qt.quit();
          }
    }
    
    to:
     MouseArea {
         anchors.fill: parent
         onClicked: page.state = ''
     }
    
    The expression sets the state to the base state and returns the image to its initial position.

In the Navigator pane, copy topLeftRect (by pressing Ctrl+C) and paste it to the canvas twice (by pressing Ctrl+V). Qt Creator renames the new instances of the element topLeftRect1 and topLeftRect2.

Copy_topLeftRect_twice.png

Select topLeftRect1 and edit its properties:

  1. In the Id field, enter middleRightRect.
  2. In Layout, select the vertical center anchor button and then the right anchor button to anchor the rectangle to the middle right margin of the screen.
  3. In the Margin field, select 10 for the right anchor and 0 for the vertical center anchor.
  4. In the code editor,add a pointer to a clicked expression to the mouse area element. The following expression sets the state to State1:
    onClicked: page.state = 'State1'
    You will create State1 later.
middleRightRect.png

Select topLeftRect2 and edit its properties:

  • In the Id field, enter bottomLeftRect.
  • In Layout, select the bottom and left anchor buttons to anchor the rectangle to the bottom left margin of the screen.
  • In the Margin field, select 20 for the bottom anchor and 10 for the left anchor.
  • In the code editor, add a pointer to a clicked expression to the mouse area element. The following expression sets the state to State2:
    onClicked: page.state = 'State2'
    You will create State2 later.
  • bottomLeftRect.png


    Here is the 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'
            }
        }
    }
    

    Press Ctrl+S to save the changes.


    Press Ctrl+R to run the application.

    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.


    This tutorial is based on Creating a Qt Quick Application.

    It seemed straight forward to follow the tutorial at first, however, there were couple of missing pieces, at least for me. I just hope this tutorial fills the gap for you if any.


    This will be continued, please visit Creating QML Animation B:

    1. Adding Views
    2. Adding Animation to the View





    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