0

我尝试在我的 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)

还有截图
QCalendarWidget 截图

4

1 回答 1

0

问题在于多边形的第二个点:由于您是根据 center 设置它x因此表格的每个新列都将具有不同的坐标参考。

如果你只是打印矩形,你会看到结果:

    def paint(self, painter, option, index):
        # ...
        if index.row() == 1:
            print(index.column(), rect.center())

这将输出:

>>> 0 PyQt5.QtCore.QPoint(15, 41)
>>> 1 PyQt5.QtCore.QPoint(46, 41)
>>> 2 PyQt5.QtCore.QPoint(77, 41)
>>> 3 PyQt5.QtCore.QPoint(108, 41)
>>> 4 PyQt5.QtCore.QPoint(139, 41)

由于您每次都添加一半的“中心x”坐标,结果是每次您获得基于非零列的新矩形时,第二个点都会变得越来越远。

如果您希望该角从矩形宽度的 75% 开始,只需使用右上角作为参考,然后从中减去/添加相对坐标 (QPoint):

        ref = rect.topRight()
        corner = QPolygon([
            ref, 
            ref + QPoint(-rect.width() / 4, 0), 
            ref + QPoint(0, rect.height() / 2)
        ])
于 2021-06-22T16:29:03.653 回答