Skip to content Skip to sidebar Skip to footer

I Want To Put The Text In Pyqt Qcalendarwidget

I would like to put the text of p.drawText (r.x () + 10, r.y () + 33, '{} / {}'. Format ('tset1', 'test2') condition on the selected QCalendarWidget date. But it is not good. impor

Solution 1:

You have to overwrite the paintCell() method since this method is called in paintEvent():

classCalendarWidget(QCalendarWidget):
    defpaintCell(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()


classmain_window(QWidget):
    def__init__(self):
        super(main_window, self).__init__()
        self.resize(1280, 900)
        self.Calendar()

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

classCalendarWidget(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)defon_clicked(self, date):
        self._selected_dates.add(date)

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

Post a Comment for "I Want To Put The Text In Pyqt Qcalendarwidget"