[英]“QPainter::begin: Paint device returned engine == 0, type: 1” with PySide
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对象后,出现错误消息:
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
. 如果要在窗口小部件内绘制某些东西,则需要使用窗口小部件的
paintEvent
来定义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()
. 此方法允许为即时绘画声明
Qpainter
,并且通过这种方式避免调用Qpainter.begin()
和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 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().
警告:当paintdevice是窗口小部件时,QPainter只能在paintEvent()函数内部或paintEvent()调用的函数中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.