用qml写app时,我发现qml的日历很麻烦,于是自己写了一个紧凑的时间选择控件
具体效果如下
在使用自定义参数实现布局和检索数据的同时,可以选择是否需要年份
1、获取当前时间Component.onCompleted: {
var date=新日期(;
year.curTime=date.getFullYear (;
month.curTime=date.getMonth () 1;
day.curTime=date.getDate (;
}
现在的月份需要1
2、外部接口
(1、获取当前时间property int h_offset: 0; //横向偏移值(在横向中心) ) )。
属性int top margin : 0; //顶部距离
propertyboolisneedyear : true; //是否需要年份
属性字符串时间: '; 用于获取TextField中的时间
) 2、获取当前时间
时间选择控件的接口
属性int curtime : 0; //当前时间
属性int maxtime : 0; //最大时间
属性int mintime : 0; //最小时间
3、实现单独时间选择按钮//中间时间按钮
导入Qt quick 2.0
Rectangle{
属性int curtime : 0; //当前时间
属性int maxtime : 0; //最大时间
属性int mintime : 0; //最小时间
width: 55
高清: 81;
Column{
anchors.fill: parent;
Rectangle{
id: add_btn;
width: parent.width;
height: parent.height/3;
color:'#b9f0fb ';
border.width: 1;
border.color: '#8be3f5';
文本{
anchors.centerIn: parent;
text: ';
font.bold: true;
color:'#3ccfed ';
font.pixelSize: 20;
}
//按钮
MouseArea{
id:add_mouse;
anchors.fill: parent;
onClicked: {
if (运行时间最大)
time_text.text= curTime;
}
}
}
Rectangle{
width: parent.width;
height: parent.height/3;
文本{
id:time_text;
anchors.centerIn: parent;
文本: curtime;
color:'#999797 ';
font.pixelSize: 18;
}
}
//-按钮
Rectangle{
id:sub_btn;
width: parent.width;
height: parent.height/3;
color:'#b9f0fb ';
border.width: 1;
border.color: '#8be3f5';
文本{
anchors.centerIn: parent;
text: '-';
font.bold: true;
color:'#3ccfed ';
font.pixelSize: 20;
}
MouseArea{
id:sub_mouse;
anchors.fill: parent;
onClicked: {
周期时间(if ) )。
time_text.text=--curTime;
}
}
}
}
//btn的点击效果
states: [
状态{
name:'btn_add_pressed '
when: add_mouse.pressed;
属性更改{
target:add_btn;
color:Qt.Darker('#09C3E9',1.2 );
}
(,
状态{
name:'btn_sub_pressed '
when: sub_mouse.pressed;
属性更改{
target:sub_btn;
color:Qt.Darker('#09C3E9',1.2 );
}
}
]
}
具体效果是这样的,同时时间控件也通过curTime获取相应的时间
4、总体控制结构
不贴整体的代码。 很长。 主要用三个Rectangle的上面的标题、下面的btn的背景,还有整个背景,中间用row布局时间选择按钮。 最上面是TextField。
5、最后贴下载链接
代码比较简单,我不想传递github