Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt处理大量对象的速度很慢_Python_Performance_Pyqt_Qgraphicsitem_Qmainwindow - Fatal编程技术网

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

Python PyQt处理大量对象的速度很慢,python,performance,pyqt,qgraphicsitem,qmainwindow,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__(s

我在这里提供了一个简单的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_())
导入系统 从PyQt4导入QtGui、QtCore 从PyQt4.QtCore导入* 从PyQt4.QtGui导入* 项目=无 类矩形(QGraphicsItem): def u uu init uu uuuu(self,parent,x,y,width,height,scene=None,fill=None): self.parent=parent 超级(矩形,自我)。\uuuu初始化 self.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable | QGraphicsItem.itemisselect) rect=QRectF(x,y,宽度,高度) self.dimen=[x,y,宽度,高度] self.rect=rect 场景.附加项(自身) self.show_它的ui() #函数获取相对位置 def获取相对位置(自身、loc): self.loc=QPointF(loc[0],loc[1]) 全局项 self.loc=self.mapFromItem(item,self.loc) self.loc=(self.loc.x(),self.loc.y()) self.loc=列表(self.loc) self.dimen[0]=self.loc[0] self.dimen[1]=self.loc[1] #以矩形的形式显示其UI def显示其用户界面(自身): #首先获取它的相对位置并设置它。 如果(项目不是无): self.get_相对位置([self.dimen[0],self.dimen[1])) #做最后一个矩形 rect=QRectF(self.dimen[0],self.dimen[1],self.dimen[2],self.dimen[3]) self.rect=rect def boundingRect(自): 返回自校正调整(-2,-2,2,2) def parentWidget(self): 返回self.scene().views()[0] def绘制(自绘制、绘制、选项、小部件): pen=QPen(Qt.实线) 笔设置颜色(Qt.黑色) 笔设置宽度(1) 画家.镶嵌画(QtGui.QColor(255,50,90200)) painter.drawRect(self.rect) 类GraphicsView(QGraphicsView): def uuu init uuu(self,parent=None): 超级(图形视图,自我)。\uuuu初始化\uuuuu(父级) self.setDragMode(QGraphicsView.RubberBandDrag) self.setRenderInt(QPaint.AntiAlias) self.setRenderInt(qPaint.TextAntialiasing) def wheelEvent(自我,事件): 系数=1.41**(event.delta()/240.0) 自我尺度(因子,因子) 类主窗口(QtGui.QMainWindow): def uuu init uuu(self,parent=None): QtGui.QMainWindow.\uuuuu init\uuuuu(self,parent) 自宽=800 自身高度=500 self.view=GraphicsView() self.scene=qgraphicscene(self) self.scene.setscen直立(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.itemisselect) 全局项 item=self.group self.group.setFiltersChildEvents(True) self.group.setHandlesChildEvents(False) self.\u link1=矩形(self,10,10,self.Width,self.Height,self.scene) self.\u link1.setFiltersChildEvents(真) self._link1.setHandlesChildEvents(False) self.group.addToGroup(self.\u link1) self.scene.addItem(self.group) self.addObjects() #在这里,我在大画布中添加了对象 def addObjects(self): xpos=20 ypos=20 #将下面的数字分别改为5 宽度=50 高度=50 #生成许多矩形 而(ypos<450): xpos=20 而(xpos<750): t=矩形(self.\u link1、xpos、ypos、宽度、高度、self.scene、True) self.group.addToGroup(t) xpos+=(宽度+2) ypos+=(高度+2) app=QtGui.QApplication(sys.argv) mainWindow=mainWindow() 方法=主窗口 mainWindow.show() sys.exit(app.exec_())
尝试将mainwindow类的addObjects函数中的宽度和高度值从50分别更改为5和5。然后,当您平移/缩放时,整个应用程序以非常慢的速度运行。请分享您对纠正此问题的建议。

在OPENGL中尝试一下如何??如果使用OPENGL,是否可以提高性能(至少是平移和缩放部分)