You have to overwrite the paintCell()
method since this method is called in paintEvent():
class CalendarWidget(QCalendarWidget):
def paintCell(self, painter, rect, date):
super(CalendarWidget, self).paintCell(painter, rect, date)
if date == self.selectedDate():
painter.save()
f = QFont()
f.setPixelSize(10)
f.setBold(True)
f.setItalic(True)
painter.setFont(f)
r = rect
painter.drawText(
rect.topLeft() + QPoint(10, 33),
"{}/{}".format("tset1", "test2"),
)
painter.restore()
class main_window(QWidget):
def __init__(self):
super(main_window, self).__init__()
self.resize(1280, 900)
self.Calendar()
def Calendar(self):
self.cal = CalendarWidget(self)
self.cal.resize(500, 500)
Update:
If you want the text to remain, you must save the date and repaint if necessary since Qt repaints everything
class CalendarWidget(QCalendarWidget):
def __init__(self, parent=None):
super(CalendarWidget, self).__init__(parent)
self._selected_dates = set()
self._selected_dates.add(self.selectedDate())
self.clicked.connect(self.on_clicked)
@pyqtSlot(QDate)
def on_clicked(self, date):
self._selected_dates.add(date)
def paintCell(self, painter, rect, date):
super(CalendarWidget, self).paintCell(painter, rect, date)
if date in self._selected_dates:
painter.save()
f = QFont()
f.setPixelSize(10)
f.setBold(True)
f.setItalic(True)
painter.setFont(f)
r = rect
painter.drawText(
rect.topLeft() + QPoint(10, 33),
"{}/{}".format("tset1", "test2"),
)
painter.restore()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…