[英]How to improve QGraphicsView performance in a 2D static scene with many items? (no way to solve it?)
If understood correctly, QGraphicsView is supposed to handle million items efficiently. 如果理解正确,则QGraphicsView应该可以有效处理数百万个项目。
In my application, I only have some few thousand and the performance is already very bad. 在我的应用程序中,我只有几千个,性能已经很差。 When the View is showing the whole scene, zooming, hoverEnvents and any other stuff becomes impossible.
当View显示整个场景时,将无法进行缩放,hoverEnvents和其他任何操作。
I have tried to create a parent-child relationship among items, and different optimization flags, but still the same result. 我试图在项目之间创建父子关系,并创建不同的优化标志,但结果仍然相同。 I really hope that I'm making some stupid mistake, but after several days looking for a way to solve the problem, I did not find any solution.
我真的希望我犯了一些愚蠢的错误,但是在寻找解决问题的方法几天后,我没有找到任何解决方案。
I would really appreciate any help! 我将非常感谢您的帮助!
This reproduces my problem: 这重现了我的问题:
import sys
import random
from PyQt4.QtGui import *
NO_INDEX = False
OPTIMIZE = False
ITEM_COORD_CACHE = False
ITEM_DEVICE_CACHE = False
NESTED_ITEMS = False
class TestItem(QGraphicsEllipseItem):
def paint(self, painter, option, index):
return QGraphicsEllipseItem.paint(self, painter, option, index)
def hoverEnterEvent (self, e):
self.setBrush(QBrush(QColor("orange")))
def hoverLeaveEvent(self,e):
self.setBrush(QBrush(None))
if __name__ == '__main__':
n = int(sys.argv[1]) # Number of items. With 5000 I already
# have performance problems
app = QApplication(sys.argv)
scene = QGraphicsScene()
# Populates scene
prev = None
for i in xrange(n):
# Random geometry and position
r1 = random.randint(10, 100)
r2 = random.randint(10, 100)
x = random.randint(0, 500)
y = random.randint(0, 500)
item = TestItem(x, y, r1*2, r2*2)
item.setAcceptsHoverEvents(True)
if NESTED_ITEMS:
# Creates a parent child structure among items
if not prev:
scene.addItem(item)
else:
item.setParentItem(prev)
prev = item
else:
scene.addItem(item)
if ITEM_COORD_CACHE:
item.setCacheMode(QGraphicsItem.ItemCoordinateCache)
elif ITEM_DEVICE_CACHE:
item.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
# Creates View
view = QGraphicsView(scene)
# Sets basic Flags for nice rendering
view.setRenderHints(QPainter.Antialiasing or QPainter.SmoothPixmapTransform)
if NO_INDEX:
view.setItemIndexMethod(QGraphicsScene.NoIndex);
if OPTIMIZE:
view.setOptimizationFlags(QGraphicsView.DontAdjustForAntialiasing
or QGraphicsView.DontClipPainter
or QGraphicsView.DontSavePainterState)
view.show()
sys.exit(app.exec_())
Basically, what you can play with are cache modes and update modes, but also the size of the bsp-tree of the scene. 基本上,您可以使用的是缓存模式和更新模式,还有场景的bsp树的大小。 Also, this speech from DevDays 2010 gives some hints and tips: http://qt.nokia.com/developer/learning/online/talks/developerdays2010/tech-talks/qt-graphics-view-in-depth .
此外,DevDays 2010的演讲还提供了一些提示和技巧: http : //qt.nokia.com/developer/learning/online/talks/developerdays2010/tech-talks/qt-graphics-view-in-depth 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.