Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML DATAEDIT



  • Элемент доделал, но остался единственным багом с месяцами: число он нормально отрабатывает, а если будет месяц и стоит 06, то получается 16, то он сбрасывается. Как исправить?

    import QtQuick 2.0
    import QtQuick.Controls 1.4
    
    
    
    
    
    Item {
        id: dateEdit
    
        readonly property date currentDate: calendar.selectedDate
        readonly property bool isCalendarOpened: calendar.visible
        property string color: "lightblue"
    
        signal calendarOpened;
        signal calendarClosed;
    
        width: textField.width + btnOpen.width
        height: textField.height
    
        Column {
            id: column
            spacing: 2
    
            Row {
                spacing: 5
                TextField {
                    id: textField
                    text: Qt.formatDate(calendar.selectedDate, "dd.MM.yyyy");
                    //                readOnly: true
    
                    function validate_date(value)
                    {
                        var arrD = value.split(".");
                        arrD[1] -= 1;
                        var d = new Date(arrD[2], arrD[1], arrD[0]);
                        if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) {
                            return true;
                        } else {
    
                            return false;
                        }
                    }
    
                    function getMonth(value)
                    {
                        var arrm = value.split(".");
                        arrm[1] -= 0;
                            return arrm[1];
    
                    }
    
                    function getDay(value)
                    {
                        var arrd = value.split(".");
                        arrd[0] -= 0;
                            return arrd[0];
    
                    }
    
                    Keys.onReleased:
                   {validate_date(text)?console.log("Current data: "+(text.substring(3,5))):{text=Qt.formatDate(calendar.selectedDate, "dd.MM.yyyy")}}
                    inputMask: "99.99.9999"
                    inputMethodHints: Qt.ImhDigitsOnly
                    Keys.onUpPressed:  calendar.__selectNextDay()
                    Keys.onDownPressed: calendar.__selectPreviousDay()
                }
                Rectangle {
                    id: btnOpen
                    radius: 5
                    width: 30
                    height: textField.height
                    color: dateEdit.color
    
                    Text {
                        text: "*"
                    }
    
                    MouseArea {
                        anchors.fill: parent
    
                        onClicked: {
                            dateEdit.isCalendarOpened ?  closeCalendar() : openCalendar()
                        }
                    }
                }
            }
    
            Calendar {
                id: calendar
                visible: false
                dayOfWeekFormat: Locale.ShortFormat
                onClicked: {
                    closeCalendar()
                }
    
                function open()
                {
                    visible = true
                }
    
                function close()
                {
                    visible = false
                }
            }
        }
    
        function openCalendar()
        {
            calendar.open()
            calendarOpened()
        }
    
        function closeCalendar()
        {
            calendar.close()
            calendarClosed()
        }
    }
    


  • Доделал

    import QtQuick 2.0
    import QtQuick.Controls 1.4
     
    Item {
        id: dateEdit
     
        readonly property date currentDate: calendar.selectedDate
        readonly property bool isCalendarOpened: calendar.visible
        property string color: "lightblue"
     
        signal calendarOpened;
        signal calendarClosed;
     
        width: textFielddata.width + btnOpen.width
        height: textFielddata.height
     
        Column {
            id: column
            spacing: 2
     
            Row {
                spacing: 5
                TextField {
                    id: textFielddata
                    text: Qt.formatDate(calendar.selectedDate, "dd.MM.yyyy");
                    //                readOnly: true
     
                    function validate_date(value)
                    {
                        var arrD = value.split(".");
                        arrD[1] -= 1;
                        var d = new Date(arrD[2], arrD[1], arrD[0]);
                        if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) {
                            return true;
                        } else {
     
                            return false;
                        }
                    }
     
     
                    validator: RegExpValidator { regExp: /^([0-2]?[1-9]|3[0-9]).(0?[1-9]|1[0-9]).([0-9][0-9][0-9][0-9])$ / } // /^([0-2]?[1-9]|3[0-1]).(0?[1-9]|1[0-2]).([0-9][0-9][0-9][0-9])$ /
                    Keys.onReleased:
                    {if (validate_date(text)) {console.log("Current data: "+(text))} else if ([text[3]].toString() == "1") {text=text.substring(0,4)+0+text.substring(5);textFielddata.cursorPosition=5} else if ([text[0]].toString() == "3"&&([text[1]].toString()!="0")&&([text[1]].toString()!="1"))  {text=text.substring(0,1)+0+text.substring(2);textFielddata.cursorPosition=1} else {text="01"+text.substring(2);textFielddata.cursorPosition=0}}
                    inputMask:"99.99.9999";
                    inputMethodHints: Qt.ImhDigitsOnly
                    Keys.onUpPressed:  calendar.__selectNextDay()
                    Keys.onDownPressed: calendar.__selectPreviousDay()
                }
                Rectangle {
                    id: btnOpen
                    radius: 5
                    width: 30
                    height: textFielddata.height
                    color: dateEdit.color
     
                    Text {
                        text: "*"
                    }
     
                    MouseArea {
                        anchors.fill: parent
     
                        onClicked: {
                            dateEdit.isCalendarOpened ?  closeCalendar() : openCalendar()
                        }
                    }
                }
            }
     
            Calendar {
                id: calendar
                visible: false
                dayOfWeekFormat: Locale.ShortFormat
                onClicked: {
                    closeCalendar()
                }
     
                function open()
                {
                    function get_date(value)
                    {
                        var arrD = value.split(".");
                        arrD[1] -= 1;
                        var d = new Date(arrD[2], arrD[1], arrD[0]);
                        if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) {
                            return d;
                        } else {
     
                            return false;
                        }}
     
                    calendar.selectedDate=new Date(get_date(textFielddata.text));
                    visible = true
     
                }
     
                function close()
                {
                    visible = false
                }
            }
        }
     
        function openCalendar()
        {
            calendar.open()
            calendarOpened()
        }
     
        function closeCalendar()
        {
            calendar.close()
            calendarClosed()
        }
    }
     
    Вызов
     
    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.5
    import QtQuick.Controls 2.5
     
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("QML DATAEDIT тест")
     
        QmlDateEdit{
     
        }
     
    }
     
    


  • Доработка функции:
    function open()
    {
    function get_date(value)
    {
    var arrD = value.split(".");
    arrD[1] -= 1;
    var d = new Date(arrD[2], arrD[1], arrD[0]);
    if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) {
    return d;
    } else {

                        return false;
                    }}
    
                calendar.selectedDate=new Date(get_date(textFielddata.text));
                visible = true
    
            }

Log in to reply