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?
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With