用QML测量经过的时间

mar*_*trz 6 time qt qml qt-quick qtquick2

让我们考虑以下示例:我们有一个Qt Quick Controls Button.用户在5秒内点击两次.在Button第一次推送之后,QML Timer正在运行这5秒钟.我们希望测量两次点击之间经过的时间,精确度为毫秒.

不幸的是,QML Timer无法向我们显示经过的时间.

正如BlackBerry论坛上所建议的那样,可以比较日期.但是,这不是很方便,因为第一次点击可能发生在31 Dec 2015, 23:59:55第二次点击,第二次点击1 Jan 2016, 00:00:05,检查必须是复杂的.

还有更好的选择吗?

BaC*_*Zzo 12

正如评论中所解释的那样,QML Timer不适合您的特定需求,因为它与动画计时器同步(此处有更多详细信息),因此其分辨率也取决于动画计时器.

@qCring解决方案肯定令人满意,如果需要更高的精度或更好的性能,我更喜欢这种方法(另请参阅此答案以及底部关于提高精度的有趣链接).

但是,根据您的要求, QML/JS方法是完全可行的.在这种情况下,您可以利用JavaScript Date,因为它很容易计算使用时间getTime(),但也因为QML完全支持JS Date并且还使用一些有用的函数扩展它.

这是一个简单的例子:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3

ApplicationWindow {
    width: 300
    height: 300
    visible: true

    property double startTime: 0

    ColumnLayout {
        anchors.fill: parent

        Text {
            id: time
            font.pixelSize: 30
            text: "--"
            Layout.alignment: Qt.AlignCenter
        }

        Button {
            text: "Click me!"
            Layout.alignment: Qt.AlignCenter

            onClicked: {
                if(startTime == 0){
                    time.text = "click again..."
                    startTime = new Date().getTime()
                } else {
                    time.text = new Date().getTime() - startTime + " ms"
                    startTime = 0
                }
            }
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)


luf*_*ffy 6

您没有在问题中提及测量的时间是否仅用于调试目的,或者是否需要用于其他计算。因为如果不是,QML 提供了一种非常简单的方法来调试使用console.time("id string")和进行各种操作所花费的时间console.timeEnd("id string")

使用 a 的示例Button如下所示:

Button {
    text: "click here"
    property bool measuring: false
    onClicked: {
        if(!measuring){
            console.time("button")
            measuring=true
        } else {
            console.timeEnd("button")
            measuring=false
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这将以毫秒为单位打印时间到控制台,并且对于测量在 QML 中执行一些长操作所需的时间非常有用。


qCr*_*ing 5

不幸的是,QML Timer没有提供检查已用时间的属性.但您可以用C++编写自定义Timer并将其公开给QML:

MyTimer.h

#include <QObject>
#include <QElapsedTimer>

class MyTimer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int elapsed MEMBER m_elapsed NOTIFY elapsedChanged)
    Q_PROPERTY(bool running MEMBER m_running NOTIFY runningChanged)
private:
    QElapsedTimer m_timer;
    int m_elapsed;
    bool m_running;
public slots:
    void start() {
        this->m_elapsed = 0;
        this->m_running = true;

        m_timer.start();
        emit runningChanged();
    }

    void stop() {
        this->m_elapsed = m_timer.elapsed();
        this->m_running = false;

        emit elapsedChanged();
        emit runningChanged();
    }

signals:
    void runningChanged();
    void elapsedChanged();
};
Run Code Online (Sandbox Code Playgroud)

通过qmlRegisterType<MyTimer>("MyStuff", 1, 0, "MyTimer")它在QML中注册后注册:

Window.qml

import QtQuick 2.4
import QtQuick.Controls 1.3
import MyStuff 1.0

ApplicationWindow {
    width: 800
    height: 600
    visible: true

    Button {
        id: button
        anchors.centerIn: parent
        text: timer.running ? "stop" : "start"
        checkable: true

        onClicked: {
            if (timer.running) {
                timer.stop()
                label.text = timer.elapsed + "ms"
            } else { 
                timer.start()
            }
        }

        MyTimer {
            id: timer
        }
    }

    Text {
        id: label
        anchors.left: button.right
        anchors.verticalCenter: button.verticalCenter
        text: "0ms"
        visible: !timer.running
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!