I want to put the text in pyqt QCalendarWidget

谁说胖子不能爱 提交于 2021-02-05 08:31:15

问题


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.

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class main_window(QWidget):
    def __init__(self):
        super(main_window, self).__init__()
        self.resize(1280, 900)

        self.Calendar() 

    def Calendar(self):
        self.cal = QCalendarWidget(self)    
        self.cal.resize(500, 500)
        self.cal.clicked.connect(self.Calendar_click)

    def Calendar_click(self):
        p = QPainter()
        r = QRect(0,0,10,10)
        d = self.cal.selectedDate()
        self.cal.paintCell(p, r, d)
        if (d == QDate.currentDate()):      
            f = QFont()
            f.setPixelSize(10)
            f.setBold(True)
            f.setItalic(True)
            p.setFont(f)
            p.drawText(r.x()+10, r.y()+33, '{}/{}'.format('tset1','test2'))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = main_window()
    main.show()

I've tried many, but I still do not know how to put small text on the selected date.


回答1:


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


来源:https://stackoverflow.com/questions/55673665/i-want-to-put-the-text-in-pyqt-qcalendarwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!