开发者

How to improve QGraphicsView performance in a 2D static scene with many items? (no way to solve it?)

开发者 https://www.devze.com 2023-02-05 18:07 出处:网络
If understood correctly, QGraphicsView is supposed to handle million items efficiently. In my application, I only have some few thousand and the performance is already very bad. When the View is sho

If understood correctly, QGraphicsView is supposed to handle million items efficiently.

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.

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:
               开发者_C百科 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_())
  • Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
  • nVidia Corporation G84 [Quadro FX 1700]
  • Ubuntu 9.04 64 bits
  • qt4 4.5.3
  • python-qt4 4.6


Basically, what you can play with are cache modes and update modes, but also the size of the bsp-tree of the scene. 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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号