QML 实现日期范围选择框

日期范围选择只需要组合几个组件就行了,着实很简单,这里只是水一篇。

有两个要注意的地方, 弹框的 closePolicy 可以设置为 Popup.CloseOnPressOutsideParent| Popup.CloseOnReleaseOutsideParent| Popup.CloseOnEscape,这样点击弹框关联的编辑框的时候弹框也消失了;另一点就是 js Date 对象是个引用类型,所以每次使用我都 new 一个,避免重复使用被影响。

实现效果:(日历弹框暂时用的 Control1 的日历)

主要实现代码:

(github链接:https://github.com/gongjianbo/QmlComponentStyle/tree/master/CustomComponent )

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls 1.4 as Old

//日期范围选择
//(使用Control1的日历实现)
Rectangle {
    id:control
    implicitWidth: 250
    implicitHeight: 30

    property var beginDate //起始时间
    property var endDate //截止时间
    property bool __selectedBegin: true //当前选中的是起始时间吗
    property date __minDate  //最小可选时间
    property date __maxDate  //最大可选时间

    property color normalTextColor: "#3D3E40"
    property color unselectTextColor: normalTextColor
    property color selectTextColor: "#305FDE"

    property alias font: item_center.font
    property alias beginText: item_begin.text
    property alias endText: item_end.text

    Component.onCompleted: {
        let min_date=new Date();
        min_date.setFullYear(1949);
        __minDate=min_date;
        let max_date=new Date();
        max_date.setFullYear(3049);
        __maxDate=max_date;
    }
    radius: 4
    border.color: "#D8DCE6"

    //起始日期
    Text {
        id: item_begin
        anchors{
            verticalCenter: parent.verticalCenter
            left: parent.left
            right: item_center.left
            leftMargin: 5
        }
        height: parent.height
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter

        text: beginDate
              ?Qt.formatDate(beginDate,"yyyy-MM-dd")
              :""
        font: control.font
        color:!item_pop.visible
              ?normalTextColor
              :__selectedBegin
                ?selectTextColor
                :unselectTextColor

        MouseArea{
            anchors.fill: parent
            onClicked: {
                __selectedBegin=true;
                selectBeginChange();
                item_pop.open();
            }
        }
    }

    Text{
        id: item_center
        anchors.centerIn: parent
        text: "-"
        color: normalTextColor
        font{
            pixelSize: 14
            family: "Microsoft YaHei"
        }
    }

    //截止日期
    Text {
        id: item_end
        anchors{
            verticalCenter: parent.verticalCenter
            left: item_center.right
            right: parent.right
            rightMargin: 5
        }
        height: parent.height
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
        text: endDate
              ?Qt.formatDate(endDate,"yyyy-MM-dd")
              :""
        font: control.font
        color:!item_pop.visible
              ?normalTextColor
              :!__selectedBegin
                ?selectTextColor
                :unselectTextColor

        MouseArea{
            anchors.fill: parent
            onClicked: {
                __selectedBegin=false;
                selectBeginChange();
                item_pop.open();
            }
        }
    }

    Popup{
        id:item_pop
        width: 250
        height: 250
        y:control.height+1
        closePolicy:Popup.CloseOnPressOutsideParent|
                    Popup.CloseOnReleaseOutsideParent|
                    Popup.CloseOnEscape
        background: Old.Calendar{
            id:item_calendar
            anchors.fill: parent
            focus: true
            onClicked: {
                selectDate();
            }
            onDoubleClicked: {
                selectDate();
                item_pop.close();
            }
            function selectDate(){
                if(item_pop.visible){
                    if(control.__selectedBegin)
                        control.beginDate=selectedDate;
                    else
                        control.endDate=selectedDate;
                }
            }
        }
    }

    function selectBeginChange(){
        if(__selectedBegin){
            item_calendar.selectedDate=beginDate
            item_calendar.minimumDate=__minDate;
            item_calendar.maximumDate=endDate;
        }else{
            item_calendar.selectedDate=endDate
            item_calendar.minimumDate=beginDate;
            item_calendar.maximumDate=__maxDate;
        }
    }

    function clear(){
        beginDate=undefined;
        endDate=undefined;
    }
}
        Row{
            spacing: 10
            //日期范围选择
            CustomDateRange{
                id: date_range
                height: 40
                width: 300
            }
            Button{
                text: "\u8fd1\u4e00\u5468" //"近一周"
                onClicked: {
                    let begin_date=new Date();
                    date_range.endDate=new Date();
                    begin_date.setDate(begin_date.getDate()-6)
                    date_range.beginDate=begin_date;
                }
            }
            Button{
                text: "\u8fd1\u4e00\u6708" //"近一月"
                onClicked: {
                    let begin_date=new Date();
                    date_range.endDate=new Date();
                    begin_date.setMonth(begin_date.getMonth()-1);
                    date_range.beginDate=begin_date;
                }
            }
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龚建波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值