Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Kaemer1645 opened this issue Feb 3, 2025 · 0 comments

Comments

@Kaemer1645
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant