Skip to content

High CPU Usage due to adjustWidgetSize() in paintEvent Method of QCustomCheckBox #127

@Kaemer1645

Description

@Kaemer1645

Hello,

I've encountered a performance issue with the QCustomCheckBox class in the QT-PyQt-PySide-Custom-Widgets library. Specifically, the problem is related to the paintEvent method as shown in the code below:

def paintEvent(self, e: QPaintEvent):
    super().paintEvent(e)
    opt = QStyleOption()
    opt.initFrom(self)
    painter = QPainter(self)
    painter.setRenderHint(QPainter.Antialiasing)
    self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)

    # SET PEN
    painter.setPen(Qt.NoPen)

    # Define margins
    margin = 3

    # DRAW ICON (Optional)
    if not self.icon.isNull():
        icon_size = self.height() * 0.7 if self.icon.availableSizes() else QSize(16, 16)  
        if self._iconSize == QSize(0, 0):
            self._iconSize = QSize(icon_size, icon_size)
        
        pixmap = self.icon.pixmap(self._iconSize)
        icon_x = self.height() * 2.1 + margin
        icon_y = (self.height() - self._iconSize.height()) / 2
        painter.drawPixmap(icon_x, icon_y, pixmap)

    if not self.isChecked():
        painter.setBrush(QColor(self.bgColor))
        painter.drawRoundedRect(0, 0, self.height() * 2.1, self.height(), self.height() * .5, self.height() * .5)
        painter.setBrush(QColor(self._circleColor))
        painter.drawEllipse(self.pos, 0, self.height(), self.height())
    else:
        painter.setBrush(QColor(self._activeColor))
        painter.drawRoundedRect(0, 0, self.height() * 2.1, self.height(), self.height() * .5, self.height() * .5)
        painter.setBrush(QColor(self._circleColor))
        painter.drawEllipse(self.pos, 0, self.height(), self.height())
    
    self.adjustWidgetSize()  # Suspected Line

    painter.end()

The adjustWidgetSize() method call within the paintEvent causes the CPU usage to permanently increase to around 20%. This has significant performance implications.

Is the use of adjustWidgetSize() within paintEvent necessary for the widget's functionality? Shouldn't it be moved to another method?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions