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

How to convert QPainter(self) to QImage?



  • I want to apply blur to a version of my painted circle to be placed under the original circle for additional effect. Because I couldn't find a way to directly apply a blur filter to QPainter, or the widget, I found this method:

    blur = QtWidgets.QGraphicsBlurEffect()
    blur.setBlurRadius(20)
    result = applyEffectToImage(img, blur)
    result.save(r"N:/sample.bmp")
    
            
    def applyEffectToImage(src, effect):
        scene = QtWidgets.QGraphicsScene()
        item = QtWidgets.QGraphicsPixmapItem()
        item.setPixmap(QPixmap.fromImage(src))
        item.setGraphicsEffect(effect)
        scene.addItem(item)
        res = QImage(src.size(), QImage.Format_ARGB32)
        res.fill(Qt.transparent)
        ptr = QPainter(res)
        scene.render(ptr, QRectF(), QRectF(0,0, src.width(), src.height()) )
        return res
    

    But if I create an img and pass it to QPainter, it will have the same artifact shown here:
    https://forum.qt.io/topic/111004/how-to-mask-a-widget-by-a-single-color-for-transparency/14

    So I must use QPainter(self).

    How can I convert my QPainter(self) to a QImage?

    I just want to draw a circle (which I did), blur it, place this blurry version under the original circle. Is there a simpler or better way to do this other than this?

    Thanks in advance.



  • QPainter(self) is highly dependent upon what self is. Have you reviewed the QPainter and QPaintDevice documentation?



  • You seem to not understand what QPainter is or what it does, QPainter is a painter who paints on a canvas(self object) that can be a QImage, QPixmap, QWidget, etc. so you cannot convert the painter into QImage, what you can do is convert the canvas to QImage.



  • From what I understand you you want to have a QImage that shows a circle where the filter blur was applied. I am right?


Log in to reply