QImage 转换时 PIL 图像到 PixMap 错误

问题描述 投票:0回答:1

以下代码块:

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)

扩大小部件尺寸时工作正常,但缩小时会产生伪影?

enter image description here

python qpainter qimage qpixmap
1个回答
0
投票

发布问题后不久我就找到了答案。 QImage 是使用未初始化的数据创建的。

添加这个就可以了: self.image.fill(0)

© www.soinside.com 2019 - 2024. All rights reserved.