QPixmap 损坏的图像

QPixmap corrupt image

为什么我的图像在使用简单的 QPixMap 和绘图命令显示时损坏。每隔一段时间它就会正确显示。

self._pixmap = QtGui.QPixmap(128,128)
painter = QtGui.QPainter(self._pixmap)
brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
brush.setColor(QtGui.QColor(240, 20, 20, 255))
painter.setPen(QtGui.QPen(brush, 1, QtCore.Qt.SolidLine,QtCore.Qt.SquareCap))
painter.drawLine(0, 0, self._pixmap.width(), self._pixmap.height())
painter.drawLine(self._pixmap.width(), 0, 0, self._pixmap.height())
painter.end()

你应该在使用 QPainter 绘画之前调用 fill,查看关于 QPixmap 构造函数的参考 here

This will create a PySide.QtGui.QPixmap with uninitialized data. Call PySide.QtGui.QPixmap.fill() to fill the pixmap with an appropriate color before drawing onto it with PySide.QtGui.QPainter .