<small id='dM8Fa'></small><noframes id='dM8Fa'>

    1. <tfoot id='dM8Fa'></tfoot>

        <bdo id='dM8Fa'></bdo><ul id='dM8Fa'></ul>
      <i id='dM8Fa'><tr id='dM8Fa'><dt id='dM8Fa'><q id='dM8Fa'><span id='dM8Fa'><b id='dM8Fa'><form id='dM8Fa'><ins id='dM8Fa'></ins><ul id='dM8Fa'></ul><sub id='dM8Fa'></sub></form><legend id='dM8Fa'></legend><bdo id='dM8Fa'><pre id='dM8Fa'><center id='dM8Fa'></center></pre></bdo></b><th id='dM8Fa'></th></span></q></dt></tr></i><div id='dM8Fa'><tfoot id='dM8Fa'></tfoot><dl id='dM8Fa'><fieldset id='dM8Fa'></fieldset></dl></div>
    2. <legend id='dM8Fa'><style id='dM8Fa'><dir id='dM8Fa'><q id='dM8Fa'></q></dir></style></legend>

      1. 我想把文本放在 pyqt QCalendarWidget

        时间:2023-08-05
          <i id='qryeb'><tr id='qryeb'><dt id='qryeb'><q id='qryeb'><span id='qryeb'><b id='qryeb'><form id='qryeb'><ins id='qryeb'></ins><ul id='qryeb'></ul><sub id='qryeb'></sub></form><legend id='qryeb'></legend><bdo id='qryeb'><pre id='qryeb'><center id='qryeb'></center></pre></bdo></b><th id='qryeb'></th></span></q></dt></tr></i><div id='qryeb'><tfoot id='qryeb'></tfoot><dl id='qryeb'><fieldset id='qryeb'></fieldset></dl></div>
            • <bdo id='qryeb'></bdo><ul id='qryeb'></ul>

              <small id='qryeb'></small><noframes id='qryeb'>

                <legend id='qryeb'><style id='qryeb'><dir id='qryeb'><q id='qryeb'></q></dir></style></legend>

                    <tbody id='qryeb'></tbody>
                  <tfoot id='qryeb'></tfoot>
                  本文介绍了我想把文本放在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  1. <legend id='bkAoJ'><style id='bkAoJ'><dir id='bkAoJ'><q id='bkAoJ'></q></dir></style></legend>
                      <tbody id='bkAoJ'></tbody>
                      <bdo id='bkAoJ'></bdo><ul id='bkAoJ'></ul>

                      • <small id='bkAoJ'></small><noframes id='bkAoJ'>

                        <tfoot id='bkAoJ'></tfoot>
                          <i id='bkAoJ'><tr id='bkAoJ'><dt id='bkAoJ'><q id='bkAoJ'><span id='bkAoJ'><b id='bkAoJ'><form id='bkAoJ'><ins id='bkAoJ'></ins><ul id='bkAoJ'></ul><sub id='bkAoJ'></sub></form><legend id='bkAoJ'></legend><bdo id='bkAoJ'><pre id='bkAoJ'><center id='bkAoJ'></center></pre></bdo></b><th id='bkAoJ'></th></span></q></dt></tr></i><div id='bkAoJ'><tfoot id='bkAoJ'></tfoot><dl id='bkAoJ'><fieldset id='bkAoJ'></fieldset></dl></div>