Why are changes to the QML code sometimes not refl

2020-04-21 01:00发布

问题:

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?

回答1:

This is indeed a bug in QMake. As @Mitch posted in his comment, a bug report was already filed: bugreports.qt.io/browse/QTBUG-13334

If you read through the comments in that bug report, a workaround for the problem was posted. Have been using it for a couple of months now and it seems to fix the issue. All you have to do is paste this at the end of your pro file.

#update qml
qml_scenes.depends = $$PWD/resources/scene1.qml $$PWD/resources/scene2.qml ...
qml_scenes.commands =
QMAKE_EXTRA_TARGETS += qml_scenes