Important: Please read the Qt Code of Conduct -

Clipping in a widget's paintEvent slows down the program

  • I have a problem with clipping in qt. I have a widget in which I paint a big array of rectangles. As I only change a few rectangles from time to time I want to paint only a little part of the widget new (only these rectangles), I want to clip the paint area to these parts. The isNew() function is true, if the rectangle has got a new color since the last time painting.

    @void Environment::paintEvent(QPaintEvent *event)
    QPainter painter (this);
    Tile t;

    //paint the matrix
    for(int i=0; i<size; ++
            t = matrix[i+j*yizeY];

    What am I doing wrong? My program is running even slower, when I use the setClipRegion(...) function.

  • ClipRegion is only used to make sure you don't paint outside of a boundrary you set. It will not help you speed up your app.

    I think you are misunderstanding what setClipRegion does. The same amount of objects are being rendered and the same amount of pixels will be processed. The only difference is that by setting the clipRegion, now every single pixel you try to draw will be compared against the clipRegion before it's rendered to screen. In other words you just made your application slower.

    To speed up your app, you could instead be more intelligent about what to draw. I.e instead of setting the clipRegion, you can check if your rectangle intersects with the view rect before you paint it and skip it if it does or sort your rectangles in some way that makes this easier. The QGraphicsView already has such functionality built in for instance.

  • I see my mistake, thank you.
    My idea is to draw just the rectangles, that changed their color. But how can I draw only a part of my widget? I found nothing with google, and the Documentation of QGraphicsView didn't help me. Did I miss something? I'm quite new to qt. Maybe someone has a usefull link or keywords that help me. Thanks in advance!

Log in to reply