I want to put the text in pyqt QCalendarWidget(我想把文本放在 pyqt QCalendarWidget)
问题描述
我想把 p.drawText (rx() + 10, ry() + 33, '{}/{}'.format('tset1', 'test2')的文字放code> 选择的 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.
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.
推荐答案
你必须重写 paintCell() 方法,因为这个方法是在paintEvent()中调用的:
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)
<小时>
更新:
如果您希望保留文本,则必须保存日期并在必要时重新绘制,因为 Qt 会重新绘制所有内容
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()
                        这篇关于我想把文本放在 pyqt QCalendarWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我想把文本放在 pyqt QCalendarWidget
				
        
 
            
        - 如何使用PYSPARK从Spark获得批次行 2022-01-01
 - CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
 - 我如何卸载 PyTorch? 2022-01-01
 - 我如何透明地重定向一个Python导入? 2022-01-01
 - 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
 - 使用 Cython 将 Python 链接到共享库 2022-01-01
 - ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
 - 计算测试数量的Python单元测试 2022-01-01
 - YouTube API v3 返回截断的观看记录 2022-01-01
 - 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
 
