@Rphoenix

QML TextField проверка даты?

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

spoiler
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()
}
}
  • Вопрос задан
  • 149 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы