当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Clipping in a widget's paintEvent

问题描述:

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 (only these rectangles) and clip the paint area to these parts.

The isNew() function is true if the rectangle receives new color since the previous 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];

if(t.isNew()){

painter.setClipRegion(QRegion(t.getRect()));

painter.setBrush(t.getColor());

painter.drawRect(t.getRect());

t.used();

}

}

}

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

网友答案:

If you want to improve performance, you can paint all those rectangles on a QPixmap. As you can draw to the pixmap anytime, you can draw the tiles directly to the pixmap when they needs an update. On the paint event you just draw the "already-rendered" pixmap.

That way you don't have to remember which tiles changed, and you can avoid looping through the whole matrix.

分享给朋友:
您可能感兴趣的文章:
随机阅读: