Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"QPainter::begin: Paint device returned engine == 0, type: 1"

I have the following test code:

import sys
from PySide.QtGui import *

app = QApplication(sys.argv)
widget = QWidget()
painter = QPainter(widget)

Upon creating the QPainter object, I get the error message:

QPainter::begin: Paint device returned engine == 0, type: 1

Why?

like image 233
HelloGoodbye Avatar asked Oct 25 '25 16:10

HelloGoodbye


1 Answers

If you want to draw something inside a widget, you need to use the paintEvent of the widget to define a QPainter. This method allows to declare a Qpainter for an immediat painting, and by the way it avoids a call to Qpainter.begin() and Qpainter.end().

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawLine(0, 0, 100, 100)

app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

http://doc.qt.io/qt-5/qpainter.html#details

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent().

like image 188
PRMoureu Avatar answered Oct 28 '25 06:10

PRMoureu