在QML中测量零件移动的时间

时间:2019-02-12 23:50:13

标签: qt qml qtquick2 qt-quick qtquickcontrols2

在此简单代码中,用户可以向上或向下拖动球拍。我们想知道球拍运动的时间(即 onYChanged 捕获的球拍的每个 y 变化)。如果用户快速移动它,每次 y 变化的时间自然会比他们缓慢移动它的时间短。

我这样做了,但是它总是写“ Time = 0”!请问有什么办法可以完成这个简单的任务?

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    id: window
    visible: true
    width: 700; height: 500
    color: "gray"

    Rectangle {
        id: table
        width: window.width / 1.15; height: window.height / 1.15
        y: 10
        anchors.horizontalCenter: parent.horizontalCenter
        color: "royalblue"
    }

    Racket {
        id: racket
        x: table.width - 10
        y: table.height / 2
    }
}

Racket.qml

import QtQuick 2.12

Rectangle {
    width: 15; height: 65

    property int oldY: y
    property bool yUwards: false
    property bool yDwards: false
    property double colTime

    onYChanged: {
         colTime = new Date().getTime()

        if (y > oldY)
            yDwards = true
        else if (y < oldY)
            yUwards = true
        oldY = y
        console.log("Time = ", colTime - new Date().getTime())
    }

    MouseArea {
        anchors.fill: parent
        anchors.margins: -parent.height
        drag.target: parent
        drag.axis: Drag.YAxis
        drag.minimumY: table.y
        drag.maximumY: table.height - parent.height + 10
    }
}

1 个答案:

答案 0 :(得分:1)

如果要测量更改时间,则必须执行与Y相同的过程,将最后一次时间保存在内存中:

TextView