QT QML如何将毫秒转换为时间hh:mm:ss:zzz?

时间:2019-03-24 12:20:16

标签: datetime qml

QT QML中是否有一个简单的功能,可以将hs:mm:ss:zzz格式的毫秒数转换为用户可读时间?我找到了https://doc.qt.io/qt-5/qml-qtqml-date.html文档,但我不太了解如何使用它。到目前为止,这是我的代码,但仅显示秒和millisenconds。

import QtQuick 2.8
import QtQuick.Controls 2.1


Rectangle {
id: mapItem
anchors.fill: parent

property int count: 0


    Timer {
      id: timer
      interval: 100
      running: true
      repeat: true
      onTriggered: count += 1
    }

    Text {

    text: (count / 10).toFixed(3)
    font.pixelSize: 20
    font.bold: true
    font.family: "Eurostile"}
 }

1 个答案:

答案 0 :(得分:0)

Felgo Web Editor上进行了测试,我已按预期设置了计数器的格式,请参阅下文!;

Schema::create('replies', function (Blueprint $table)
{
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id'); // <-----------
    $table->unsignedBigInteger('thread_id'); // <-----------
    $table->text('body');
    $table->timestamps();
});

请记住,如果您要设置日期格式,则可以将import Felgo 3.0 import QtQuick 2.5 App { id: app Rectangle { id: mapItem property int count: 0 Timer { id: timer interval: 10 // i set to 10 for testing for faster results, but worked on 100 also running: true repeat: true onTriggered: time.text = new Date(mapItem.count += 1).toLocaleTimeString(Qt.locale(), "hh " + "mm " + "ss " + "zzz") // you can change the formatting as you please } Text { id: time font.bold: true font.family: "Eurostile" } } } 替换为toLocaleTimeString或对每个用例而言最合适的内容。

更新!如果您要实时计数,那么您可能还希望将toLocaleDateString更改为count += 1并加上间隔,除非计时器不是实时秒数

希望这会有所帮助!