Back to Blog home

Accurate update regions for thin QGraphicsItems

Published on Tuesday January 08, 2008 by Andreas Aardal Hanssen in Labs Qt Painting Graphics View KDE | Comments

We've gotten reports about how Graphics View's performance drops in one special (yet quite common) case. It's the case when the item is fairly thin, flat, non-square, or simply sparse, where QGraphicsItem's bounding rect generates something close to worst-case updates that cause repaints of areas that really haven't changed. A very helpful customer showed me a simple testcase that illustrates this problem, which is quite hard to work around, which again effectively makes it a bug. Start the portedcanvas example, and clear the canvas (CTRL-E). Add a few meshes (ALT-M). Now click a node in the top left corner and drag it down towards the bottom right corner.

portedcanvas screenshot

Performance degrades steadily as you get closer and closer to the bottom-right corner, until the example almost grinds to a halt. The example is very busy doing something close to nothing, at least it seems so. It's redrawing the bounding rectangle of the edges to the node you're dragging.

The yellow thing you get in debug mode by setting/exporting QT_FLUSH_PAINT=1 reveals what's going on.

portedcanvas screenshot using bounding rect updates

To finally hammer this bug down, we've added an opt-in feature, QGraphicsItem::boundingRegion(), and a configurable granularity. You can tune the granularity of the bounding region of your line / edge / connector items, and with the wave of a wand, QGraphicsView will magically generate an optimal update region for that item.

So with 4.4, you'll instead get something like this:

portedcanvas screenshot using granular region updates

What you're seeing is that each of the two connector lines updates itself using a region consisting of 20-30 rectangles, a tunable granularity.

Should be in tonight's 4.4 snapshots.

Subscribe to Our Blog

Stay up to date with the latest marketing, sales and service tips and news.

The blog comment system has been migrated to a new platform. If you face any issues, please let us know via feedback@qt.io.