简体   繁体   English

PyQt5:QCalendarWidget 重置取消选择日期的字体

[英]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.我创建了一个带有样式表、当前日期文本格式的日历小部件,并通过单击 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)?如何将取消选择的(以前)日期的字体重置为默认值 - 同时使 TODAY 日期的字体(应用程序启动时应用哪种样式)保持相同(增加)?

在此处输入图像描述

Pic.1 - Current date, selected on app start. Pic.1 - 当前日期,在应用启动时选择。

在此处输入图像描述

Pic.2 - Some date clicked. Pic.2 - 点击了某个日期。 TODAY date (22nd) must stay as it is.今天的日期(22 日)必须保持原样。

在此处输入图像描述

Pic.3 - Another date clicked.图 3 - 点击了另一个日期。 TODAY date (22nd) must stay as it is, but 24th must be reset to default. TODAY 日期(22 日)必须保持原样,但 24 日必须重置为默认值。

One way to achieve what you want is to clear the current formatting by setting a null date:实现您想要的一种方法是通过设置 null 日期来清除当前格式:

    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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM