PDA

View Full Version : Need tricks and tips for Graphics framework



tonnot
21st November 2011, 14:33
I'd like to accelerate and 'manipulate' my Qgraphics 'world'
Now, I see how an item added to the graphics scene rises a boundingrect event for all the previous items.
That is, when I add the 100th item, 'somebodys' rises a boundingrect for the 99 previous. I dont understand why, theese items are currently showed.

Also, I'm going to need a method to draw a line in 'editing mode' and sincereley I dont know any fast method (I remember the easy xor method of 'basic').

I also feel the lack of usefull documentation related to improving, accelerate your drawings, etc.
It seems as I'd have to discover this 'world'.
You can find some information in the www but....
Some usefull links ? Thanks.

ChrisW67
22nd November 2011, 04:16
I have no idea what your first paragraph is talking about.

The Elastic Nodes and Diagram Scene demo both show user interaction with objects in the view. The second example also show a drag-n-draw line capability. Just start "qtdemo" and have a look under Graphics View for more examples.

tonnot
22nd November 2011, 09:05
Thanks Chris.
I'm trying to explain better.
I have write some debugs messages for the arrow item of Diagram scene.
I draw 8 'boxes' and its connecting arrows.

Ok, If I give the focus to QTcreator, clean the message window and give the focus to the diagram scene again I see the next :

bounding arrow 0
bounding arrow 0
paint arrow 0
bounding arrow 1
bounding arrow 1
paint arrow 1
bounding arrow 2
bounding arrow 2
paint arrow 2
bounding arrow 3
bounding arrow 3
paint arrow 3
bounding arrow 4
bounding arrow 4
paint arrow 4
bounding arrow 5
bounding arrow 5
paint arrow 5
bounding arrow 6
bounding arrow 6
paint arrow 6
bounding arrow 7
bounding arrow 7
paint arrow 7
bounding arrow 8
bounding arrow 8
paint arrow 8
bounding arrow 0
bounding arrow 0
paint arrow 0
bounding arrow 1
bounding arrow 1
paint arrow 1
bounding arrow 2
bounding arrow 2
paint arrow 2
bounding arrow 3
bounding arrow 3
paint arrow 3
bounding arrow 4
bounding arrow 4
paint arrow 4
bounding arrow 5
bounding arrow 5
paint arrow 5
bounding arrow 6
bounding arrow 6
paint arrow 6
bounding arrow 7
bounding arrow 7
paint arrow 7
bounding arrow 8
bounding arrow 8
paint arrow 8

'bounding arrow' is the message for 'bounding rect event' .
As you can see, there is two complete cycles (that is, the system paints things two times). And the bounding arrow is called two times before paint...

Another example of excesive bounding rect calls. First I draw 8 boxes and 8 arrows at left side of my view, set the zomm to 50% and draw two boxes at the right side. Ok, I see again how the bounding rect for the arrows from the left is called. I promise you that I have these first 8 elements at the left extreme of my 21" screen. There is no contact betwen graphic elements, so I dont understand these calls.

By now I only have seen setCacheMode () for backgrounds, or QStyleOptionGraphicsItem::exposedRect(), which works at item level. (Is there any equivalent for scene or view ?)
Which is the behavior for primitive items. In example, a QGraphicsPolygonItem() only needs the initial setPolygon() and setFlag's. I dont know if internally are a lot of bounding calls .

In short, what I'd want to know methods to avoid and/or help to Qgraphics framework does an unneccesary work.
Thanks

ChrisW67
23rd November 2011, 06:34
There is no 'bounding rect event'.

I can only assume you mean the boundingRect() function is called. The computer doesn't have magical intuition that allows it to know that two items don't overlap, it has to calculate it. Using bounding rectangles is the efficient way to determine whether arbitrarily shaped items possibly overlap (or collide if animated), which is necessary to drawing the view. Only if the bounding rectangles overlap is more expensive work done to determine the actual shape of the item should it be needed. In short, calling boundingrect() is not unnecessary work.

If you are worried about efficiency then don't do anything inefficient in your boundingRect() implementation. The bounding rectangle is generally immutable so you can cache the value.

tonnot
23rd November 2011, 07:18
Ok, thanks.