Outlaw
Outlaw

Reputation: 359

PyQt5: QCalendarWidget reset font of a de-selected date

I've created a calendar widget with its styleSheet, current date text format and set up date selection via .clicked function.

#setting up calendar
self.dateWidget = QCalendarWidget()

#setting up current date font format (on the app start)
self.painter = QTextCharFormat()
self.painter.setFont(QFont("Times", 20, 200))
self.dateWidget.setDateTextFormat(self.dateWidget.selectedDate(), self.painter)
    
#selecting current date on the app start, then connecting event for changing the date on click.
self.date = self.dateWidget.selectedDate()                      
self.dateWidget.clicked[QDate].connect(self.ChooseDate)

def ChooseDate(self, clicked=""):
    self.dateWidget.setDateTextFormat(clicked, self.painter)
    self.date = arrow.get(clicked.year(), clicked.month(), clicked.day())
    print(f'{self.date}: {self.time}')

When some date is clicked, its font gets modified.

But when another date is clicked, the previous date's font stays modified. How do I reset the font of a de-selected (previous) date back to default - at the same time leaving the font of the TODAY date (which style is applied on app start) the same (increased)?

enter image description here

Pic.1 - Current date, selected on app start.

enter image description here

Pic.2 - Some date clicked. TODAY date (22nd) must stay as it is.

enter image description here

Pic.3 - Another date clicked. TODAY date (22nd) must stay as it is, but 24th must be reset to default.

Upvotes: 1

Views: 575

Answers (1)

ekhumoro
ekhumoro

Reputation: 120608

One way to achieve what you want is to clear the current formatting by setting a null date:

    self.painter = QTextCharFormat()
    self.painter.setFont(QFont("Times", 20, 200))
    self.dateWidget.clicked[QDate].connect(self.ChooseDate)
    self.ChooseDate()

def ChooseDate(self, clicked=None):
    # clear the current formatting first
    self.dateWidget.setDateTextFormat(QDate(), self.painter)
    # then reset the current and selected dates
    self.dateWidget.setDateTextFormat(QDate.currentDate(), self.painter)
    if clicked is not None:
        self.dateWidget.setDateTextFormat(clicked, self.painter)
        self.date = arrow.get(clicked.year(), clicked.month(), clicked.day())
        print(f'{self.date}: {self.time}')
    else:
        self.date = QDate.currentDate()

Upvotes: 1

Related Questions