问题描述
我尝试在 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,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,rect,text):
if painter._date_flag:
option.displayAlignment = Qt.AlignTop | Qt.AlignLeft
super(CalendarDayDelegate,self).drawdisplay(painter,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)
还有截图
解决方法
问题在于多边形的第二个点:由于您是根据 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
”坐标的一半,结果是每次您根据非零列。
如果您希望该角从 rect 宽度的 75% 开始,只需使用右上角作为参考,然后从中减去/添加相对坐标 (QPoint):
ref = rect.topRight()
corner = QPolygon([
ref,ref + QPoint(-rect.width() / 4,0),ref + QPoint(0,rect.height() / 2)
])