In this Qt Quick simple example, I want that when I move a Racked upwards, we have a message on console.log showing that movement and another message when the Racket is moved downwards. I've written this code for that:
Racket.qml
:
import QtQuick 2.8
Rectangle {
id: root
width: 15; height: 50
x: 400; y: 100
color: "red"
property int oldY: 100
property bool yUwards: false
property bool yDwards: false
onYChanged: {
if( y > oldY) {
yDwards = true
yUwards = false
console.log("Racket moved downwards.\n")
}
else if( y < oldY) {
yDwards = false
yUwards = true
console.log("Racket moved upwards.\n")
}
oldY = y
}
MouseArea {
anchors.fill: parent
drag.target: root
drag.axis: Drag.YAxis
drag.minimumY: 10
drag.maximumY: 440
}
}
main.qml
:
import QtQuick 2.8
import QtQuick.Window 2.2
Window {
visible: true
width: 800
height: 600
Rectangle {
id: table
width: 700; height: 500
border.width: 10
border.color: "black"
color: "white"
Racket {
id: redRacket
x: 630; y: 100
color: "red"
}
}
}
When I at the first time hit Run
, the code didn't work, so I hit clean all
, run qmake
and then rebuild all
, then it worked!
How does a new QML programmer like me figure out that the code is fine but this process is needed to successfully run the program?