View Full Version : Custom QGraphicsItem being painted even when completely obscured

23rd May 2019, 20:00
Hi all - I have a qt5 app which has a QGraphicsScene into which I place a lot of Elements, a custom class which derives from QGraphicsItem. Element reimplements paint(), boundingRect(), and opaqueArea(). Several Elements should overlap and completely obscure other Elements.

However, paint() is being called for every Element in the QGraphicsScene (with QStyleOptionGraphicsItem->explosedRect always matching the full boundingRect), even the ones that will be completely obscured by other Elements.

Is there a way I can avoid painting the Elements which which shouldn't need to be painted due to being obscured?


3rd June 2019, 16:37
The reason this is important to me is that I have several Elements which are images, but the underlying data is heavily compressed. Decompressing the raw data takes a significant amount of time. So if the rendered element will be fully obscured in the final scene, I don't want to go through the work of decompressing the data.

3rd June 2019, 19:59
Can't you just cache the rendered output and just paint the cached image in the paint event?

5th June 2019, 17:17
I don't think so... I have hundreds of images being generated on the fly every second. I put them in a QGraphicsScene with a bunch of other QGraphicsItems. The user has the ability to make some of the QGraphicsItems visible/invisible at run time, so at the time the images are generated, I don't know which will be needed and which won't be needed.

Once an image does get rendered, I cache the rendered image for any repaintings. But the normal use case is to generate new images at 60fps. I do have time to render just the "top layer" of images and keep that frame rate. But because I am also spending time rendering in paint() calls for the lower (obscured) images, I'm overrunning my frame time.

5th June 2019, 17:39
60fps? and than hundreds of images at this rate? Then you should definitely think about your design. E.g. render the stuff in separate threads. But why such a high frame rate at all?
Did you take a look at https://doc.qt.io/qt-5/qgraphicsitem.html#CacheMode-enum ?

5th June 2019, 23:07
I'd like to avoid the call to paint() in the first place, so caching doesn't help. The high frame rate is because my application takes input from a connected camera which outputs at that rate. I turn its output into processed video.

Added after 8 minutes:

Thanks for the suggestions though. I'll think about some other ways I can avoid putting the QGraphicsItems in the scene in the first place

7th June 2019, 09:48
You should probably consider using a different display technology, e.g. QtQuick, which is rendered hardware accelerated on the GPU.