PyQt处理大量对象的速度很慢

时间:2014-10-30 10:34:02

标签: python performance pyqt qgraphicsitem qmainwindow

我在这里介绍一个简单的pyqt代码,但我开发了我的整个设计并且算法。问题是,PyQt在创建时无法管理大量对象。这是用于在大型矩形内绘制许多矩形的简单代码。它还具有缩放和平移功能。

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *


item = None
class Rectangle(QGraphicsItem):
def __init__(self, parent, x, y, width, height, scene = None, fill=None):
    self.parent = parent

    super(Rectangle, self).__init__()
    self.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)

    rect = QRectF(x,y,width,height)
    self.dimen = [x,y,width,height]
    self.rect = rect
    scene.addItem(self)
    self.show_its_ui()

#Function to get relative position
def get_relative_location(self,loc):
    self.loc = QPointF(loc[0],loc[1])
    global item        
    self.loc = self.mapFromItem(item, self.loc)
    self.loc = (self.loc.x(),self.loc.y())
    self.loc = list(self.loc)
    self.dimen[0] = self.loc[0]
    self.dimen[1] = self.loc[1]

#Showing its UI in the form of a rectangle
def show_its_ui(self):
    #First gets its relative location and set it.
    if(item is not None):
        self.get_relative_location([self.dimen[0],self.dimen[1]])

    #Make a Final rectangle
    rect = QRectF(self.dimen[0],self.dimen[1],self.dimen[2],self.dimen[3])        
    self.rect = rect

def boundingRect(self):
    return self.rect.adjusted(-2, -2, 2, 2)

def parentWidget(self):
    return self.scene().views()[0]


def paint(self, painter, option, widget):
    pen = QPen(Qt.SolidLine)
    pen.setColor(Qt.black)
    pen.setWidth(1)
    painter.setBrush(QtGui.QColor(255, 50, 90, 200))

    painter.drawRect(self.rect)


class GraphicsView(QGraphicsView):

def __init__(self, parent=None):
    super(GraphicsView, self).__init__(parent)
    self.setDragMode(QGraphicsView.RubberBandDrag)
    self.setRenderHint(QPainter.Antialiasing)
    self.setRenderHint(QPainter.TextAntialiasing)


def wheelEvent(self, event):
    factor = 1.41 ** (event.delta() / 240.0)
    self.scale(factor, factor)



class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
    QtGui.QMainWindow.__init__(self, parent)
    self.Width = 800
    self.Height = 500

    self.view = GraphicsView()

    self.scene = QGraphicsScene(self)
    self.scene.setSceneRect(0, 0, self.Width, self.Height)        
    self.view.setScene(self.scene)


    self.initUI()
    hbox = QtGui.QHBoxLayout()
    hbox.addWidget(self.view)

    mainWidget = QtGui.QWidget()
    mainWidget.setLayout(hbox)
    self.setCentralWidget(mainWidget)


def initUI(self):    

    self.group = QGraphicsItemGroup()        
    self.group.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
    global item
    item = self.group       
    self.group.setFiltersChildEvents(True)    
    self.group.setHandlesChildEvents(False)

    self._link1 = Rectangle(self, 10, 10, self.Width, self.Height,self.scene)
    self._link1.setFiltersChildEvents(True)
    self._link1.setHandlesChildEvents(False)

    self.group.addToGroup(self._link1)        
    self.scene.addItem(self.group)
    self.addObjects()


#Here I added objects in the big canvas
def addObjects(self):

    xpos = 20
    ypos = 20
    #Change the below to 5 each
    width = 50
    height = 50

    #Generate many rectangles
    while(ypos < 450):
        xpos = 20

        while(xpos < 750):                
            t = Rectangle(self._link1,xpos,ypos,width,height,self.scene,True)
            self.group.addToGroup(t)
            xpos += (width+2)
        ypos += (height+2)



app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
method = mainWindow
mainWindow.show()
sys.exit(app.exec_())

尝试更改主窗口类的addObjects函数中的宽度和高度值,分别从50开始分别为5和5.然后整个应用程序以非常慢的速度运行,当您进行平移/缩放时。请分享您的建议以纠正此问题。

0 个答案:

没有答案