本文介绍了对于 QGraphicsScene,在 QGraphicsItem 上使用 setZValue 会导致严重减速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt(Mac,4.7 版)通过 QGraphicsView 和 QGraphicsScene 渲染相当大的场景(来自 Open Street Maps 的地图数据).一切都很好,直到我尝试为场景中的项目设置 Z 值.我尝试了两种不同的方法来做到这一点.一方面,我只是这样做:

I'm using Qt (Mac, version 4.7) to rendering a rather large scene (map data from Open Street Maps) with QGraphicsView and QGraphicsScene. Everything works great until I try to set the Z value for the items in the scene. I've tried two separate approaches to do this. In one, I just do:

QGraphicsPathItem *item = scene->addPath(path, pen);
item->setZValue(z);

另一个我创建了自己的 QGraphicsItem 子类,但遇到了完全相同的问题.

and the other I create my own QGraphicsItem subclass, but get the exact same problem.

速度变慢的原因似乎是在 Qt 方面.一旦我设置了 z 值,场景生成需要很长时间(在任何显示之前需要几分钟,我得到旋转的死亡沙滩球),但是一旦生成,速度就会恢复正常.此外,当我尝试关闭应用程序时,它会在关闭前挂起几分钟.如果我不考虑 z 值,我不会看到任何这些问题,并且我添加了调试代码以验证它没有出现在我自己的代码中.

The cause of the slow down appears to be on the Qt side of things. It takes a very long time for the scene to generate once I set the z-value (it takes a few minutes before anything displays, and I get the spinning beach ball of death), but once it's generated, the speed is back to normal. Additionally, when I try to close the application it hangs for a few minutes before closing. I don't see any of these issues if I leave the z-value alone, and I've added debugging code to verify it's not occurring in my own code.

不幸的是,我需要设置 z 值才能正确渲染街道(例如,道路周围的轮廓、使高速公路比穿过街道的顺序更高等).

Unfortunately, I need to set the z-value in order to render streets correctly (e.g. outlines around roads, make highways a higher order than through streets, etc.).

非常感谢任何帮助!

推荐答案

您可以尝试设置 bspTreeDepth 到一个固定值.它控制场景中项目的位置.使用大量现有项目更改深度的成本可能非常高.

You may try setting bspTreeDepth to a fixed value. It controls how an item in the scene is located. Changing the depth with a large number of existing items can be very costly.

另一个优化候选是itemIndexMethod.将其设置为 noIndex 实际上可能会提高性能.

Another candidate for optimization is itemIndexMethod. Setting it to noIndex may actually increase the performance.

这篇关于对于 QGraphicsScene,在 QGraphicsItem 上使用 setZValue 会导致严重减速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:38