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

QPainter in PyQt



  • Hi guys, I kinda new with ptqt and seem to have the following error when I try to create a simple QPaint program. Does anyone know how can I "active" my Painter? Help is much appreciated. Thanks you in advance.
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::setPen: Painter not active

    def paintEvent(self, event = None):
    paint = QPainter()
    paint.setRenderHint(QPainter.Antialiasing)
    paint.setPen(QtGui.QPen(QtGui.QColor(Qt.green)))
    paint.drawEllipse(300,300,70,70)



  • Hi,

    To be active, your painter must be connected to a device, either by giving it as argument during construction or by calling QPainter::begin().



  • def paintEvent(self, event = None):
    paint = QPainter()
    paint.begin()
    paint.setRenderHint(QPainter.Antialiasing)
    paint.setPen(QtGui.QPen(QtGui.QColor(Qt.green)))
    paint.drawEllipse(300,300,70,70)
    paint.end()

    I initially had the paint.begin() statement however when i implement that statement I would have the following error:
    TypeError: QPainter.begin(QPaintDevice): not enough arguments



  • The error is giving you the answer: begin() takes a QPaintDevice (e.g. a widget) as argument.



  • What is considered as a QPaintEvent argument?


  • Lifetime Qt Champion

    Hi,

    My PyQt is a bit rusted but IIRC you need something like:

    @
    paint = QPainter(self)
    @

    Otherwise you are trying to paint on nothing so it won't work.

    Hope it helps


Log in to reply