PDA

View Full Version : QGraphicsScene with many Items requires too much memory



xdn
22nd January 2013, 17:11
Hi everyone,

I'm using a custom (inherited) QGraphicsScene to visualize a large number of 2D items. The items are custom QGraphicsLineItems with a level-of-detail threshold to paint them only at certain zoom levels. There are quite alot of them (>>500k) and they use up much more memory than I would like.
Two things came to mind that might prevent this:

1. Many of the lines are exactly the same except for their position. Is it maybe possible to somehow create only one object for a subset of lines and draw it at multiple positions in the scene?!

2. I keep only those object in memory that are actually shown to the user. Where would I implement this deleting of items that moved out of view and adding of items that are newly visible, i.e. which method of QGraphicsScene should I overload for this? Won't adding items right before a scene is updated (using addItem) in turn trigger more updates?

Both approaches do not seem very appealing so if you can think of something simpler/better please tell.

Background: The widget I'm talking about is a timeline with vertical lines at the positions of hours, minutes, seconds and frames. The lines for frames are only made visible when the view is sufficiently zoomed into the scene. The widget is supposed to support >=30fps and videos of more than 24 hours, thus the large number of line items. Is the approach of subclassing QGraphicsScene and using level-of-detail GraphicsItems even the right way to go on this?

wysota
22nd January 2013, 17:36
I would suggest to use QGraphicsScene::drawBackground() or QGraphicsScene::drawForeground() instead of those items.

Or if you do need level of detail information, then QGraphicsView has those methods as well.

xdn
22nd January 2013, 17:51
Thanks, QGraphicsScene::drawBackground() looks very much like what I wanted. I will check back once I tried it tomorrow.

xdn
23rd January 2013, 19:11
Alright, that did it. I would mark this as solved but the edit function seems to be hidden...
Thanks for the help!