我尝试在我的 QCalendarWidget 的单元格中制作一个角落。所以我制作了一个 QItemDelegate 来绘制我的三角形。
如屏幕截图所示,代理在第一列上运行良好,但在其他列上完全中断。
我不明白这是从哪里来的(我是 Qt 的新手,所以也许我做错了什么)。
这是代码:
# ---------------------------------
# ------ CalendarDayDelegate ------
class CalendarDayDelegate(QItemDelegate):
def __init__(self, parent=None, projects=None):
super(CalendarDayDelegate, self).__init__(parent=parent)
self.projects = projects
def paint(self, painter, option, index):
painter._date_flag = index.row() > 0
super(CalendarDayDelegate, self).paint(painter, option, index)
if painter._date_flag:
rect = option.rect
corner = QPolygon([
rect.topRight(),
QPoint(rect.center().x() + (rect.center().x() / 2), rect.top()),
QPoint(rect.bottomRight().x(), rect.center().y())
])
painter.save()
painter.setRenderHint(painter.Antialiasing)
painter.setBrush(QBrush(QColor(Qt.darkGreen)))
painter.setPen(QPen(QColor(Qt.darkGreen)))
painter.drawPolygon(corner)
painter.restore()
def drawDisplay(self, painter, option, rect, text):
if painter._date_flag:
option.displayAlignment = Qt.AlignTop | Qt.AlignLeft
super(CalendarDayDelegate, self).drawDisplay(painter, option, rect, text)
# ----------------------
# ------ Calendar ------
class MonthReviewCalendar(QCalendarWidget):
def __init__(self, parent=None):
super(MonthReviewCalendar, self).__init__(parent=parent)
self._init_calendar()
def _init_calendar(self):
self.setVerticalHeaderFormat(
self.verticalHeaderFormat().NoVerticalHeader
)
self.setFirstDayOfWeek(Qt.Monday)
self.calendar_view = self.findChild(
QTableView, "qt_calendar_calendarview"
)
self.calendar_delegate = CalendarDayDelegate(
projects=self._raw_projects
)
self.calendar_view.setItemDelegate(self.calendar_delegate)
还有截图