vaishak
vaishak

Reputation: 63

How to efficiently draw images using QPainter?

I want to draw a custom made Pixmap on QLabel. I have to show the image in a circle and the widget has animation on mouse hover (eg, outline color highlight, and property animation too). I tried drawing the entire image inside paintEvent of the widget. But the performance drop rate is quite unacceptable. So i have made the image cache and set the pixmap inside the paint event, but the image gets pixelated. What am i doing wrong? The same function inside paintEvent draws high quality images.

this is how i cache the pixmap

def fancyDraw(self, outlinePen):
    hasValidImage = self._validateImage(self.image)
    option = qg.QStyleOption()
    option.initFrom(self)

    x = option.rect.x()
    y = option.rect.y()
    width = option.rect.width() * self.radius
    if not hasValidImage and self.shortName:
        srcImage = qg.QPixmap(200, 200)
        srcImage.fill(qc.Qt.transparent)
        painter = qg.QPainter(srcImage)
        # painter.begin()
        painter.setRenderHint(qg.QPainter.Antialiasing)
        painter.setBrush(self._brushText)
        painter.setPen(self._penClear)
        text_path = qg.QPainterPath()
        text_width = self.font_metrics.width(self.shortName)
        text_height = self.font().pointSize()
        text_path.addText((200 - text_width) / 2,
                          (200 - ((200 - text_height)) / 2) - 1,
                          self.font(), self.shortName)
        painter.drawPath(text_path)
        painter.end()
    else:
        srcImage = qg.QPixmap(qc.QString(self.image))
    if srcImage.isNull():
        logger.error("Failed to load image %s" % self.image)
    resizedImage = srcImage.scaled(self.width(), self.height(),
                                   qc.Qt.KeepAspectRatio,
                                   qc.Qt.SmoothTransformation)
    pixmap = qg.QPixmap(width, width)
    pixmap.fill(qg.QColor("transparent"))
    imgPainter = qg.QPainter(pixmap)
    imgPainter.setRenderHint(qg.QPainter.Antialiasing)
    clip = qg.QPainterPath()
    clip.addEllipse(x + 1, y + 1, width - 2, width - 2)
    imgPainter.setClipPath(clip)
    imgPainter.drawPixmap(0, 0, width, width, resizedImage)
    imgPainter.end()
    painter = qg.QPainter(pixmap)
    painter.setRenderHint(qg.QPainter.Antialiasing)
    painter.setPen(outlinePen)
    painter.drawEllipse(x + 1, y + 1, width - 2, width - 2)
    painter.end()
    return pixmap

this is how i set the pixmap inside painter

self.normalPaint = self.fancyDraw(self._penBorder)
self.hoverPaint = self.fancyDraw(self._penBorder)
def paintEvent(self, event):
    painter = qg.QPainter(self)
    painter.drawPixmap(qc.QRect(self.x(), self.y(), self.width(), self.width()),
                       self.normalPaint)

I'm not a professional developer, just self taught. And this is my first question in stackoverflow. Sorry if something is terribly wrong. Thanks.

Upvotes: 3

Views: 1725

Answers (1)

vaishak
vaishak

Reputation: 63

this was the problem,

resizedImage = srcImage.scaled(self.width(), self.height(),
                                   qc.Qt.KeepAspectRatio,
                                   qc.Qt.SmoothTransformation)

No need to resize the image. This makes it pixelated ! Providing the full size image solved the problem.

Upvotes: 1

Related Questions