Problem
When drawing a rectangle with a line width of 1px on a QPainter with antialiasing enabled results in slightly transparent corners regardless of what JoinStyle is set. The same can be observed for polylines and QPainterPaths
def paintEvent(self, event):
with QPainter(self) as p:
p.setRenderHint(QPainter.Antialiasing, True)
p.fillRect(self.rect(), QColor('black'))
p.setBrush(Qt.NoBrush)
# 1px width border, corner is partially transparent
p.setPen(QPen(QColor("orange"), 1, cap=Qt.SquareCap, join=Qt.MiterJoin))
p.drawRect(QRectF(10.5, 10.5, 50., 25.))
# 3px width border, corner is crisp
p.setPen(QPen(QColor("orange"), 3, cap=Qt.SquareCap, join=Qt.MiterJoin))
p.drawRect(QRectF(15.5, 15.5, 50., 25.))
produces this output:
Question
Is there a way to get crisp corners for a 1px wide pen that also work for partially transparent lines and arbitrary QPainterPaths?
Solution tried so far
-
Drawing each line individually:
Works fine, but there are artifacts with partially transparent lines because the overlap at the corners. -
Drawing each line individually, but shifting start of each line by 1:
Solves issue with partially transparent lines right angles, but not other angles or curves. -
Temporarily disabling antialiasing:
Diagonal lines and curved become jagged.