PySide How to apply smoothing (antialiasing)?

There is a drawn arrow indicator. Everything works wel, but deep irregularities (burrs) remain on the red sectors. How to remove it?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from PySide6.QtGui import QPainter, QColor, QPolygon, QPaintEvent, QConicalGradient, QGradient, QBrush, QPainterPath
from PySide6.QtCore import Signal, QSize, QRect, QPoint, Qt
from PySide6.QtWidgets import QWidget
from typing import List, Tuple
class ArrowPointerIndicator(QWidget):
def __init__(self,
chunks: List[Tuple[float, QColor]],
lineWidth: int = 5,
parent: QWidget = None) ->None:
super().__init__(parent)
self.__arrowColor: QColor = QColor(255, 0, 0)
self.__gradient: QGradient = None
self.setChunks(chunks)
self.__arrow = QPolygon([
QPoint(-2, -80),
QPoint(0, -100),
QPoint(2, -80),
QPoint(2, 10),
QPoint(-2, 10),
QPoint(-2, -80)
])
self.__minimum = -10.0
self.__maximum = 10.0
self.__lineWidth = lineWidth
self.__value: float = 0.0
def arrowColor(self) ->QColor:
return self.__arrowColor
def setArrowColor(self, arrowColor: QColor) ->QColor:
if self.__arrowColor == arrowColor:
return
self.__arrowColor = arrowColor
self.update()
def setChunks(self, chunks: List[Tuple[float, QColor]]) ->None:
weightsSum: float = sum(item[0] for item in chunks)
chunks = [(item[0] / weightsSum / 2, item[1]) for item in chunks]
self.__gradient = QConicalGradient()
passedWeight = 0.25
for weight, color in chunks:
self.__gradient.setColorAt(passedWeight, color)
self.__gradient.setColorAt(passedWeight + weight - 0.0000000001, color)
passedWeight += weight
self.__gradient.setCenter(QPoint(0, 0))
self.__gradient.setAngle(-90)
self.update()
def paintEvent(self, event: QPaintEvent) ->None:
super().paintEvent(event)
drawSize = min(self.width() / 2, self.height())
painter = QPainter(self)
painter.translate(self.width() / 2, self.height())
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
self.__drawColorArc(drawSize, painter)
self.__drawArrowPointer(drawSize, painter)
def __drawColorArc(self, drawSize: int, painter: QPainter) ->None:
painter.save()
path = QPainterPath()
path.addEllipse(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize)
painter.setBrush(QBrush(self.__gradient))
drawingRect = QRect(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize)
painter.drawChord(drawingRect, 0, 180 * 16)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
painter.restore()
def __drawArrowPointer(self, drawSize: int, painter: QPainter) ->None:
painter.save()
painter.setBrush(self.__arrowColor)
painter.scale(drawSize / 100.0, drawSize / 100.0)
painter.drawConvexPolygon(self.__arrow)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
painter.restore()
if __name__ == '__main__':
from PySide6.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
window = ArrowPointerIndicator(
chunks=[
(4, Qt.GlobalColor.white),
(1, Qt.GlobalColor.red),
(1, Qt.GlobalColor.white),
(1, Qt.GlobalColor.red),
(4, Qt.GlobalColor.white),
], lineWidth=1000000)
window.show()
sys.exit(app.exec())`
[![my_indicator][1]][1]
</code>
<code>from PySide6.QtGui import QPainter, QColor, QPolygon, QPaintEvent, QConicalGradient, QGradient, QBrush, QPainterPath from PySide6.QtCore import Signal, QSize, QRect, QPoint, Qt from PySide6.QtWidgets import QWidget from typing import List, Tuple class ArrowPointerIndicator(QWidget): def __init__(self, chunks: List[Tuple[float, QColor]], lineWidth: int = 5, parent: QWidget = None) ->None: super().__init__(parent) self.__arrowColor: QColor = QColor(255, 0, 0) self.__gradient: QGradient = None self.setChunks(chunks) self.__arrow = QPolygon([ QPoint(-2, -80), QPoint(0, -100), QPoint(2, -80), QPoint(2, 10), QPoint(-2, 10), QPoint(-2, -80) ]) self.__minimum = -10.0 self.__maximum = 10.0 self.__lineWidth = lineWidth self.__value: float = 0.0 def arrowColor(self) ->QColor: return self.__arrowColor def setArrowColor(self, arrowColor: QColor) ->QColor: if self.__arrowColor == arrowColor: return self.__arrowColor = arrowColor self.update() def setChunks(self, chunks: List[Tuple[float, QColor]]) ->None: weightsSum: float = sum(item[0] for item in chunks) chunks = [(item[0] / weightsSum / 2, item[1]) for item in chunks] self.__gradient = QConicalGradient() passedWeight = 0.25 for weight, color in chunks: self.__gradient.setColorAt(passedWeight, color) self.__gradient.setColorAt(passedWeight + weight - 0.0000000001, color) passedWeight += weight self.__gradient.setCenter(QPoint(0, 0)) self.__gradient.setAngle(-90) self.update() def paintEvent(self, event: QPaintEvent) ->None: super().paintEvent(event) drawSize = min(self.width() / 2, self.height()) painter = QPainter(self) painter.translate(self.width() / 2, self.height()) painter.setRenderHint(QPainter.RenderHint.Antialiasing) self.__drawColorArc(drawSize, painter) self.__drawArrowPointer(drawSize, painter) def __drawColorArc(self, drawSize: int, painter: QPainter) ->None: painter.save() path = QPainterPath() path.addEllipse(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize) painter.setBrush(QBrush(self.__gradient)) drawingRect = QRect(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize) painter.drawChord(drawingRect, 0, 180 * 16) painter.setRenderHint(QPainter.RenderHint.Antialiasing) painter.restore() def __drawArrowPointer(self, drawSize: int, painter: QPainter) ->None: painter.save() painter.setBrush(self.__arrowColor) painter.scale(drawSize / 100.0, drawSize / 100.0) painter.drawConvexPolygon(self.__arrow) painter.setRenderHint(QPainter.RenderHint.Antialiasing) painter.restore() if __name__ == '__main__': from PySide6.QtWidgets import QApplication import sys app = QApplication(sys.argv) window = ArrowPointerIndicator( chunks=[ (4, Qt.GlobalColor.white), (1, Qt.GlobalColor.red), (1, Qt.GlobalColor.white), (1, Qt.GlobalColor.red), (4, Qt.GlobalColor.white), ], lineWidth=1000000) window.show() sys.exit(app.exec())` [![my_indicator][1]][1] </code>
from PySide6.QtGui import QPainter, QColor, QPolygon, QPaintEvent, QConicalGradient, QGradient, QBrush, QPainterPath
from PySide6.QtCore import Signal, QSize, QRect, QPoint, Qt
from PySide6.QtWidgets import QWidget
from typing import List, Tuple

class ArrowPointerIndicator(QWidget):
    def __init__(self,
                 chunks: List[Tuple[float, QColor]],
                 lineWidth: int = 5,
                 parent: QWidget = None) ->None:
        super().__init__(parent)
        self.__arrowColor: QColor = QColor(255, 0, 0)
        self.__gradient: QGradient = None
        self.setChunks(chunks)
        self.__arrow = QPolygon([
            QPoint(-2, -80),
            QPoint(0, -100),
            QPoint(2, -80),
            QPoint(2, 10),
            QPoint(-2, 10),
            QPoint(-2, -80)
        ])
        self.__minimum = -10.0
        self.__maximum = 10.0
        self.__lineWidth = lineWidth
        self.__value: float = 0.0
    def arrowColor(self) ->QColor:
        return self.__arrowColor  
    def setArrowColor(self, arrowColor: QColor) ->QColor:
        if self.__arrowColor == arrowColor:
            return
        self.__arrowColor = arrowColor
        self.update()
    def setChunks(self, chunks: List[Tuple[float, QColor]]) ->None:
        weightsSum: float = sum(item[0] for item in chunks)
        chunks = [(item[0] / weightsSum / 2, item[1]) for item in chunks]
        self.__gradient = QConicalGradient()
        passedWeight = 0.25
        for weight, color in chunks:
            self.__gradient.setColorAt(passedWeight, color)
            self.__gradient.setColorAt(passedWeight + weight - 0.0000000001, color)
            passedWeight += weight
        self.__gradient.setCenter(QPoint(0, 0))
        self.__gradient.setAngle(-90)
        self.update()

    def paintEvent(self, event: QPaintEvent) ->None:
        super().paintEvent(event)
        drawSize = min(self.width() / 2, self.height())
        painter = QPainter(self)
        painter.translate(self.width() / 2, self.height())
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        self.__drawColorArc(drawSize, painter)
        self.__drawArrowPointer(drawSize, painter)
    def __drawColorArc(self, drawSize: int, painter: QPainter) ->None:
        painter.save()
        path = QPainterPath()
        path.addEllipse(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize)
        painter.setBrush(QBrush(self.__gradient))
        drawingRect = QRect(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize)
        painter.drawChord(drawingRect, 0, 180 * 16)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        painter.restore()
    def __drawArrowPointer(self, drawSize: int, painter: QPainter) ->None:
        painter.save()
        painter.setBrush(self.__arrowColor)
        painter.scale(drawSize / 100.0, drawSize / 100.0)
        painter.drawConvexPolygon(self.__arrow)
       painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        painter.restore()

if __name__ == '__main__':
    from PySide6.QtWidgets import QApplication
    import sys
    app = QApplication(sys.argv)
    window = ArrowPointerIndicator(
            chunks=[
                (4, Qt.GlobalColor.white),
                (1, Qt.GlobalColor.red),
                (1, Qt.GlobalColor.white),
                (1, Qt.GlobalColor.red),
                (4, Qt.GlobalColor.white),
        ], lineWidth=1000000)
    window.show()
    sys.exit(app.exec())`



[![my_indicator][1]][1]

I applied Antialiasing wherever there is Painter. Tried different combinations, but it looks like smoothing doesn’t work here.

I had to place the code very tightly, sorry for the inconvenience.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật