QML自定义时间选择器

在进行使用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.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值