以下代码块:
class MDraw(QtWidgets.QGraphicsView):
def __init__(self, parent):
super(MDraw, self).__init__(parent)
self.data = None
self.width = 0
self.height = 0
def resizeEvent(self, event):
self.width = event.size().width()
self.height = event.size().height()
self.draw(self.data)
def draw(self, indata):
self.image = QImage(self.width, self.height, QImage.Format_ARGB32)
self.image = QImage(self.width, self.height, QImage.Format_Grayscale8)
self.image = QImage(self.width, self.height, QImage.Format_Alpha8)
self.image = QImage(self.width, self.height, QImage.Format_RGB32)
qpainter = QPainter(self.image)
bbush = QBrush(QColor(90,0,0))
qpainter.setBrush(bbush)
qpainter.drawRect(20, 20, 80, 80)
qpainter.end()
self.pixmap = QPixmap.fromImage(self.image)
self.pixmapItem = QGraphicsPixmapItem(self.pixmap)
scene = QGraphicsScene()
scene.addItem(self.pixmapItem)
self.setScene(scene)
扩大小部件尺寸时工作正常,但缩小时会产生伪影?
发布问题后不久我就找到了答案。 QImage 是使用未初始化的数据创建的。
添加这个就可以了: self.image.fill(0)