【问题标题】:How to improve QGraphicsView performance in a 2D static scene with many items? (no way to solve it?)如何在具有许多项目的 2D 静态场景中提高 QGraphicsView 性能? (没有办法解决?)
【发布时间】:2011-06-13 02:00:17
【问题描述】:

如果理解正确,QGraphicsView 应该可以有效地处理百万个项目。

在我的应用程序中,我只有几千个,性能已经很差了。当 View 显示整个场景时,缩放、hoverEnvents 和任何其他东西都变得不可能。

我尝试在项目和不同的优化标志之间创建父子关系,但结果仍然相同。我真的希望我犯了一些愚蠢的错误,但是经过几天寻找解决问题的方法,我没有找到任何解决方案。

非常感谢任何帮助!

这重现了我的问题:

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_())
  • Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
  • nVidia Corporation G84 [Quadro FX 1700]
  • Ubuntu 9.04 64 位
  • qt4 4.5.3
  • python-qt4 4.6

【问题讨论】:

  • 我在最近的 PyQt5.7 上进行了尝试,没有发现性能影响,而且 CPU 速度更快,省略了 50,000 个椭圆。我猜 Qt 现在更好了,如果它现在出现,问题可能会以不同的方式解决。

标签: python qt4 pyqt4 qgraphicsview qgraphicsitem


【解决方案1】:

基本上,您可以使用缓存模式和更新模式,还有场景的 bsp-tree 的大小。此外,来自 DevDays 2010 的这篇演讲给出了一些提示和技巧:http://qt.nokia.com/developer/learning/online/talks/developerdays2010/tech-talks/qt-graphics-view-in-depth

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 2022-11-07
    • 2021-01-20
    • 2015-01-27
    • 2012-09-19
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多