在进行使用qml编程过程中,需要实现日期时间的选择。
但是qml官方只提供日期控件,因此自己定义了时间控件
注意 QtQuick 2.9不要降低版本
否则会有内部的信号处理器无法使用
import QtQuick 2.9
import QtQuick.Controls 2.3
Rectangle
{
property int hour: 0;
property int minute: 0;
property string hour_str: hour_str_area.text;
property string minute_str: minute_str_area.text;
width: 100;
height: 140;
color: "white";
Rectangle
{
id: shi;
x: 10;
y: 10;
width: 30;
height: 30;
Text {
text: "时";
anchors.centerIn: parent;
font.pointSize: 20;
}
}
Rectangle
{
id: add1;
anchors.top: shi.bottom;
anchors.left: shi.left;
anchors.right: shi.right;
height: shi.height;
color: "red";
Text
{
text: qsTr("+");
font.pointSize: 20;
anchors.centerIn: parent;
}
MouseArea
{
anchors.fill: parent;
onClicked:
{
hour++;
if(hour>23)
hour=0;
if(hour<10)
hour_str_area.text="0"+hour
else
hour_str_area.text=hour;
}
}
}
Rectangle
{
id: hour_show;
anchors.top: add1.bottom;
anchors.left: add1.left;
anchors.right: add1.right;
height: hour_str_area.height;
TextInput {
id: hour_str_area
text: hour;
validator: IntValidator{bottom: 0; top: 23;}
font.pointSize: 20;
anchors.centerIn: parent;
onTextEdited:
{
hour=text;
}
}
}
Rectangle
{
id: subtract1;
anchors.left: add1.left;
anchors.right: add1.right;
anchors.top: hour_show.bottom;
height: add1.height;
color: add1.color
Text
{
text: qsTr("-");
font.pointSize: 20;
anchors.centerIn: parent;
}
MouseArea
{
anchors.fill: parent;
onClicked:
{
hour--;
if(hour<0)
hour=23;
if(hour<10)
hour_str_area.text="0"+hour
else
hour_str_area.text=hour;
}
}
}
Rectangle
{
id: fen;
anchors.left: shi.right;
anchors.leftMargin: 10;
anchors.top: shi.top;
height: shi.height;
width: shi.width;
Text {
text: "分";
anchors.centerIn: parent;
font.pointSize: 20;
}
}
Rectangle
{
id: add2;
anchors.left: fen.left;
anchors.right: fen.right;
anchors.top: fen.bottom;
height: add1.height;
color: add1.color
Text
{
text: qsTr("+");
font.pointSize: 20;
anchors.centerIn: parent;
}
MouseArea
{
anchors.fill: parent;
onClicked:
{
minute++;
if(minute>59)
minute=0;
if(minute<10)
minute_str_area.text="0"+minute;
else
minute_str_area.text=minute;
}
}
}
Rectangle
{
id: minute_show;
anchors.top: hour_show.top;
anchors.left: fen.left;
anchors.right: fen.right;
anchors.bottom: hour_show.bottom;
TextInput {
id: minute_str_area
text: minute;
validator: IntValidator{bottom: 0; top: 59;}
font.pointSize: 20;
anchors.centerIn: parent;
onTextEdited:
{
minute=text;
}
}
}
Rectangle
{
id: subtract2;
anchors.top: subtract1.top;
anchors.bottom: subtract1.bottom;
anchors.left: fen.left;
anchors.right: fen.right;
color: subtract1.color;
Text
{
text: qsTr("-");
font.pointSize: 20;
anchors.centerIn: parent;
}
MouseArea
{
anchors.fill: parent;
onClicked:
{
minute--;
if(minute<0)
minute=59;
if(minute<10)
minute_str_area.text="0"+minute;
else
minute_str_area.text=minute;
}
}
}
}
以上代码遵循WTFPL协议
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004Copyright © 2004 Sam Hocevar <sam@hocevar…net>Everyone is permitted to copy and distribute verbatim or modifiedcopies of this license document, and changing it is allowed as longas the name is changed…DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSETERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION0. You just DO WHAT THE FUCK YOU WANT TO.