PDA

View Full Version : [Solved] Scene-level item-related custom painting



akiross
4th May 2011, 19:32
Hello there,
I'm using the Graphics View Framework.

I've a scene with many, many items. Most of them luckily aren't visible. I need to draw something like a "force field" around every object. The problem is that such a field shall be globally-calculated, so I've to take the items visible (which shall be pretty fast given BSP), do some calculations, and draw somehow the "force field". It shall be painted behind the items.

Of course the force field will be bigger than an item, so I will always paint in an area bigger than item's bounding box.

Now the question is: which is the best (fastest? easiest? more qt-appropriate?) way to draw my "force field"?

I thought of some options:
1. paint pixel-by-pixel my force field on a drawable surface (QPixmap? QImage? Which is the more appropriate?) with the size of the viewport and then use drawBackground and invalidate to paint that image. I'm not sure if drawBackground is conceived to be used like this, and the use of invalidate() is kinda suspicious from my point of view.

2. paint pixel-by-pixel as above, but using an item with low z-value, positioned at 0.0, sized as big as the view is, using painter->drawImage to draw this item.

3. paint the "force field" relative to each item in that item's paint() method, and resize the item's bounding rect OR just calling update() when necessary. Item-wise painting seems good to me, but changing bounding rect is just wrong and calling update() may require some explicit caching in some cases.

Note that background "force field" has to be recalculated on items position change or when the view is changed. It can be cached in the other cases.

Which way is the best to you, and why?
Thanks in advance!
~Aki

wysota
4th May 2011, 20:09
If each item has its own force field then implement it as a QGraphicsEffect subclass, it has all you need.

akiross
4th May 2011, 21:55
Wow thanks!! I never noticed it and seems to fit perfectly! yay! :D

EDIT:
I didn't knew about graphics effect at all... This will allow me to easily achieve new interaction features I didn't think of before. Wysota, I'll raise a statue in your honor.

Added after 1 29 minutes:

How is it possible to assign a single effect instance to multiple items? I tried, but the ownership seems to change.