Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to force antialiasing on "translation only" drawPixmap?



  • Hi all, when I call painter.drawPixmap() to render a pixmap to another pixmap, it does not always render antialiased.

    Here's the code. I'm using PyQt5 with Python3, but it shouldn't matter.

    class MyWidget(QtWidgets.QWidget):
        def __init__(self, parent):
            super(MyWidget, self).__init__(parent)
    
        def paintEvent(self, event):
            # paint the source image
            pixmap_orig = QtGui.QPixmap(10, 10)
            pixmap_orig.fill(QtCore.Qt.transparent)
            painter = QtGui.QPainter(pixmap_orig)
            painter.drawRect(0, 0, 9, 9)
            painter.end()
    
            # testing
            painter = QtGui.QPainter(self)
            painter.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
            painter.drawPixmap(QtCore.QRectF(10, 10, 10, 10), pixmap_orig, QtCore.QRectF(0, 0, 10, 10))
            painter.drawPixmap(QtCore.QRectF(10.5, 30, 10, 10), pixmap_orig, QtCore.QRectF(0, 0, 10, 10))
            painter.drawPixmap(QtCore.QRectF(10, 50, 10, 11), pixmap_orig, QtCore.QRectF(0, 0, 10, 10))
            painter.drawPixmap(QtCore.QRectF(10.5, 70, 10, 11), pixmap_orig, QtCore.QRectF(0, 0, 10, 10))
            painter.end()
    

    Result: (8x enlarged for easy viewing)
    result picture

    The 2nd square is drawn at (10.5, 30), 0.5px right from the 1st square. But it actually appears at (11, 30) with no antialiased border.
    The 3rd square is stretched to 10 x 11 and its left/right border is very clear.
    The 4th square is also 0.5px off, left/right border is antialiased.

    My question is: why doesn't the 2nd square render antialiased? how to force it to render antialiased? (I'm sure this is what I want)

    BTW painter.paintEngine().type() is 10 (i.e. QPaintEngine::Raster)

    Thanks!


Log in to reply