【问题标题】:How to update a QPixmap in a QGraphicsView with PyQt如何使用 PyQt 更新 QGraphicsView 中的 QPixmap
【发布时间】:2012-11-20 04:37:53
【问题描述】:

我正在尝试在 QGraphicsView 内的 QPixmap 上绘画。绘画工作正常,但 QGraphicsView 没有更新它。

这是一些工作代码:

#!/usr/bin/env python

from PyQt4 import QtCore
from PyQt4 import QtGui

class Canvas(QtGui.QPixmap):
    """ Canvas for drawing"""
    def __init__(self, parent=None):
        QtGui.QPixmap.__init__(self, 64, 64)
        self.parent = parent
        self.imH = 64
        self.imW = 64
        self.fill(QtGui.QColor(0, 255, 255))
        self.color = QtGui.QColor(0, 0, 0)

    def paintEvent(self, point=False):
        if point:
            p = QtGui.QPainter(self)
            p.setPen(QtGui.QPen(self.color, 1, QtCore.Qt.SolidLine))
            p.drawPoints(point)

    def clic(self, mouseX, mouseY):
        self.paintEvent(QtCore.QPoint(mouseX, mouseY))    

class GraphWidget(QtGui.QGraphicsView):
    """ Display, zoom, pan..."""
    def __init__(self):
        QtGui.QGraphicsView.__init__(self)
        self.im = Canvas(self)
        self.imH = self.im.height()
        self.imW = self.im.width()
        self.zoomN = 1            
        self.scene = QtGui.QGraphicsScene(self)
        self.scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
        self.scene.setSceneRect(0, 0, self.imW, self.imH)
        self.scene.addPixmap(self.im)
        self.setScene(self.scene)
        self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QtGui.QGraphicsView.AnchorViewCenter)
        self.setMinimumSize(400, 400)
        self.setWindowTitle("pix")

    def mousePressEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            pos = self.mapToScene(event.pos())
            self.im.clic(pos.x(), pos.y())
            #~ self.scene.update(0,0,64,64)
            #~ self.updateScene([QtCore.QRectF(0,0,64,64)])
            self.scene.addPixmap(self.im)
            print('items')
            print(self.scene.items())
        else:
            return QtGui.QGraphicsView.mousePressEvent(self, event)

    def wheelEvent(self, event):
        if event.delta() > 0:
            self.scaleView(2)
        elif event.delta() < 0:
            self.scaleView(0.5)

    def scaleView(self, factor):
        n = self.zoomN * factor
        if n < 1 or n > 16:
            return
        self.zoomN = n
        self.scale(factor, factor)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    widget = GraphWidget()
    widget.show()
    sys.exit(app.exec_())

mousePressEvent 在 QPixmap 上做了一些绘画。但是我发现更新显示的唯一解决方案是创建一个新实例(这不是一个好的解决方案)。

我该如何更新它?

【问题讨论】:

    标签: python pyqt repaint qgraphicsview qpixmap


    【解决方案1】:

    像素图无法链接到您的场景,该项目使用它的内部副本,因此您必须使用新像素图更新QGraphicsPixmapItem

    def __init__(self):
        ...
        # save the item as a member
        self.imItem = self.scene.addPixmap(self.im) 
        ...
    
    def mousePressEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            pos = self.mapToScene(event.pos())
            self.im.clic(pos.x(), pos.y())
            self.imItem.setPïxmap(self.im)
        ...
    

    但是让你的类Canvas 继承自QGraphicsPixmapItem 而不是QPixmap 会更有意义,你仍然需要获取带有pixmap() 的像素图,在上面绘画,然后调用setPixmap 来更新它。作为奖励,该代码将在项目自己的 mousePressEvent 方法中。

    【讨论】:

      猜你喜欢
      • 2014-05-05
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2011-09-09
      • 2015-08-19
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多