PDA

View Full Version : Best way to draw the same thing many times in a QGraphicsScene



wayfaerer
9th May 2012, 01:42
My basic question is this: If you have many graphic objects (e.g., red circles) to draw that are the same in appearance but different in location, is it better to have one composite QGraphicsItem that draws the shape in many locations, or to have one QGraphicsItem for every instance?

Here's the context: I'm trying to draw grid lines for a musical sequencer (e.g., the vertical lines in this pic (http://www.vstplanet.com/Other_tools_photos/Full%20view/MusicStudioProducer.jpg)). These are just thin vertical lines occurring in a regular pattern.

I've thought about making one composite QGraphicsItem whose paint() function draws all of the lines in a loop. The boundingRect() function would just return the scene rect, since the lines should appear to extend infinitely high and low, and should be repeated throughout the entire width of the scene rect. This is very simple to implement, but I wonder if it might slow things down because the painter will draw all of the lines in the entire scene whenever a repaint is performed, even if only a few of the lines are visible.

I've also thought about having one QGraphicsItem for every line. I can use the flyweight pattern to share as much data as possible (e.g., pen, boundingRect) between the lines, but still, QGraphicsItem does require enough memory to cause a slow-down whenever the width of the scene is increased and more lines must be dynamically allocated.

Can someone help clear up if one of these approaches is better than the other? Or maybe I should be doing something completely different? Thanks!

littletux
9th May 2012, 12:46
Hi,

if it is only grid lines, you could also consider overwriting QGraphicsScene::drawForeground() (or, in your case,
QGraphicsScene::drawBackground() since the gray bars should obviously be behind the scene items).

- Andreas

wayfaerer
9th May 2012, 23:10
That's a great idea. I don't know why I didn't think of it.