如何提高 QGraphicsView 在具有许多项目的 2D 静态场景中的性能? (没有办法解决吗?)

发布于 2024-10-13 15:29:25 字数 2453 浏览 6 评论 0 原文

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

在我的应用程序中,我只有几千个,性能已经很糟糕了。当视图显示整个场景时,缩放、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

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:
                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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2024-10-20 15:29:26

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

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文