I'm using a QDateEdit control in a GUI in Python. I want to control it just by using keyboard, so when I press the up arrow key it changes from 31-jan-2000 to 01-feb-2000, by instance.
Currently, it just changes year, month and day independiently, but I want to change it like a block, increasing by calendar days.
In the gif, you could see the behavior of the control, I changed day, month and year independently, but I couldn't change it as a "whole date".
Thanks in advance.
It is possible override the default behaviour by reimplementing stepBy
:
class DateEdit(QtGui.QDateEdit):
def stepBy(self, steps):
self.setDateTime(self.dateTime().addDays(steps))
However, this doesn't quite work perfectly, because the cursor must be in the year section to get continuous increments. If it's in the month section , it will only increment through all months/days in the year; and if it's in the day section , it will only increment through all days in the month. Personally, I think I would treat this as a "feature", and leave it at that (since the implementaion is so simple).
You could try to force the cursor to stay in the year section , but that would prevent manual editing, which significantly reduces usability. However, I suppose you could use the calendar-popup to provide manual editing, and then make the line-edit read-only:
class DateEdit(QtGui.QDateEdit):
def __init__(self, *args, **kwargs):
super(DateEdit, self).__init__(*args, **kwargs)
self.setCalendarPopup(True)
edit = self.lineEdit()
edit.setReadOnly(True)
edit.selectionChanged.connect(lambda edit=edit: edit.end(False))
def stepBy(self, steps):
self.setDateTime(self.dateTime().addDays(steps))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.