def validateDate(self, item): """ Does a two-stage check if user gives a new date as input. First, QRegExpValidator checks if the input matches the regular expression '\d{1,2}\.', i.e. a one- or two-digit number followed by a dot. If this is accepted, a QDate with the new day is created. If this is valid (depends on the month, internally handled by QDate), the new date is assigned to the item. If any of those validations fails, the user is prompted with a warning and the date is reset. :param item | DateItem :return valid | bool """ state = _DATEVALIDATOR_.validate(item.text(), 0)[0] if state == QtGui.QValidator.Acceptable: newDay = unicode(item.text()) #str 'dd. year, month, _ = item.data().toDate().getDate() date = QDate(year, month, int(newDay[:-1])) #skip trailing . of day if date.isValid(): item.setData(date) return True QtGui.QMessageBox.warning( None, 'Invalid input!', 'Please enter a valid day of the format \'dd.\'') item.setText(str(item.data().toDate().day()) + '.')
def setDate(self, date): if isinstance(date, datetime.date): date = QDate(date.year, date.month, date.day) if date is not None and date.isValid(): self._line_edit.setText(str(date.toString("yyyy-MM-dd"))) else: self._line_edit.setText("")