获取到系统的当前时间,然后在qml的界面上面显示出来,首先,我们生成一个Text文本来放置时间的显示部件。
然后利用qml的Timer来获取到系统的时间,再让Text显示即可
上代码:
生成一个Text文本
Text{
id:nowTimes
font.pixelSize: 25;
anchors.top:parent.top
anchors.topMargin:5
anchors.right: parent.right
anchors.rightMargin: 100
// anchors.horizontalCenter: parent.horizontalCenter
font.family: “微软雅黑”;
color: “red”
}
然后,使用Timer来触发事件
Timer {
id:nowTimer;
interval: 1000;
running: true;
repeat: true
onTriggered: {
nowTimes.text = getCurDate()//此处即是让时间显示到文本中去
}
}
我们还需要获取到系统时间的方法
function getCurDate()
{
var d = new Date();
var years = d.getFullYear();
var month = add_zero(d.getMonth()+1);
var days = add_zero(d.getDate());
var hours = add_zero(d.getHours());
var minutes = add_zero(d.getMinutes());
var seconds=add_zero(d.getSeconds());
var ndate = years+"/"+month+"/"+days+" “+hours+”:"+minutes+":"+seconds;
return ndate;
}
function add_zero(temp)
{
if(temp<10) return “0”+temp;
else return temp;
}
这上面两个function就是获取到时间的功能,这样之后,就能够在qml的界面上显示出系统的时间了
使用qml来画界面,显示时间
最新推荐文章于 2025-03-01 04:41:28 发布