代码之家  ›  专栏  ›  技术社区  ›  Franky

以qml为单位测量部件移动的时间

  •  0
  • Franky  · 技术社区  · 6 年前

    在这个简单的代码中,用户可以拖动 球拍 向上或向下。我们想知道 球拍 移动(即,每个 Y 被抓球拍的变化 假名 )如果用户移动得很快 Y 变化比他们自然地缓慢移动的时候要小。

    我喜欢这个,但它总是写“时间=0”!有什么方法可以完成这个简单的任务吗?

    MQ.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
        }
    }
    

    小精灵 :

    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
        }
    }
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Yonghwan Shin    6 年前

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

    import QtQuick 2.12
    
    Rectangle {
        width: 15; height: 65
    
        property bool yUwards: false
        property bool yDwards: false
        property real oldY: y
        property double last_time: new Date().getTime()
    
        onYChanged: {
            var current_time = new Date().getTime()
            console.log("Time = ", current_time - last_time)
            if (y > oldY)
                yDwards = true
            else if (y < oldY)
                yUwards = true
            oldY = y
            last_time = current_time
        }
    
        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
        }
    }