QGraphics Item创建绘画延迟

时间:2018-01-17 08:33:23

标签: python pyqt4 qgraphicsview qgraphicsscene qgraphicsitem

我尝试使用QGraphicsItem创建2D绘图,我成功地做到了这一点,但是当我拖动Q​​GraphicsItem时会出现延迟并且视图失真。

enter image description here

在搜索解决方案时,我遇到了QGraphicsItem paint delay。我将 mouseMoveEvent 应用于我的QGraphicsView,但它没有解决问题。

有人可以告诉我导致问题的原因以及如何解决?

这是我的代码:

from PyQt4 import QtGui, QtCore
import sys
import numpy as np

class MyGraphicsItem(QtGui.QGraphicsItem):

    def __init__(self,dataX,dataY):
        super(MyGraphicsItem,self).__init__()
        self.Xval = dataX
        self.Yval = dataY
        self.Xvalmin = np.min(self.Xval)
        self.Xvalmax = np.max(self.Xval)
        self.Yvalmin = np.min(self.Yval)
        self.Yvalmax = np.max(self.Yval)
        self.rect = QtCore.QRectF(0,0,100,2)
        self.points = []
        self._picture = None

    def paint(self, QPainter, QStyleOptionGraphicsItem, QWidget_widget=None):
        if self._picture is None:
            self._picture = QtGui.QPicture()
            QPainter.begin(self._picture)
            startPoint = QtCore.QPointF(0, 0)
            cubicPath = QtGui.QPainterPath()
            cubicPath.moveTo(startPoint)

            for i in range(len(self.points) - 2):
                points_ = self.points[i:i+3]
                cubicPath.cubicTo(*points_)

            QPainter.setPen(QtGui.QPen(QtCore.Qt.red))
            QPainter.drawLine(0,10,100,10)
            QPainter.drawLine(0,-10,0,10)
            QPainter.setPen(QtGui.QPen(QtCore.Qt.black))
            QPainter.drawPath(cubicPath)
            QPainter.end()
        else:
            self._picture.play(QPainter)


    def boundingRect(self):
        return self.rect


class mygraphicsview(QtGui.QGraphicsView):

    def __init__(self):
        super(mygraphicsview,self).__init__()

    def mouseMoveEvent(self, event):
        QtGui.QGraphicsView.mouseMoveEvent(self,event)
        if self.scene().selectedItems():
            self.update()


class Mainwindow(QtGui.QMainWindow):

    def __init__(self):
        super(Mainwindow,self).__init__()
        self.main_widget = QtGui.QWidget()
        self.vl = QtGui.QVBoxLayout()
        self.scene = QtGui.QGraphicsScene()
        self.view = mygraphicsview()

        self.Xval = np.linspace(0,100,1000)
        self.Yval = np.sin(self.Xval)

        self.painter = QtGui.QPainter()
        self.style = QtGui.QStyleOptionGraphicsItem()
        self.item = MyGraphicsItem(self.Xval, self.Yval)
        self.item.paint(self.painter, self.style,self.main_widget)

        self.item.setFlag(QtGui.QGraphicsItem.ItemIsMovable,True)
        self.trans = QtGui.QTransform()
        self.trans.scale(5,5)
        self.item.setTransform(self.trans)
        self.scene = QtGui.QGraphicsScene()
        self.scene.addItem(self.item)

        self.view.setScene(self.scene)
        self.vl.addWidget(self.view)
        self.main_widget.setLayout(self.vl)
        self.setCentralWidget(self.main_widget)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = Mainwindow()
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我解决了拖延延迟的问题。

发生这种延迟的原因是由于boundinRect()函数。 boudingRect围绕设计的项目太紧了。

将一些marigin添加到boundingRect(),解决了这个问题。

self.rect = QtCore.QRectF(-10,-10,120,25)