Time for action – Dragging an item around

Create a new Qt Quick Application - Empty project. Edit the main.qml file to add a circle to the window:

Rectangle {
    id: circle
    width: 60; height: width
    radius: width / 2
    color: "red"
} 

Next, add a MouseArea to the circle and use its drag property to enable moving the circle by touch (or mouse):

Rectangle {
    //...
    MouseArea {
        anchors.fill: parent
        drag.target: circle
    }
}

Then, you can start the application and begin moving the circle around.

Get Game Programming using Qt 5 Beginner's Guide - Second Edition 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.