Problem with erasing of rectangle area in paintEvent
-
I need to draw two overlapping rects. Both with transparent.
Some thing like this: http://itmages.ru/image/view/1133740/8fdac9c4
So we have one rect with transparent under other without transparent, then we group it and set transparent to group (example made in Inkscape).I tries to make it in qt, but have some issues when I paint it in widget which over parent.
First way:
@ QPainter p(this);
// widget border
p.drawRect(0, 0, width()-1, height()-1);
p.setBrush(Qt::lightGray);
p.setOpacity(0.3);
p.drawRect(0, 0, 200, 200);
p.setOpacity(1);
p.eraseRect(100, 100, 200, 200);
p.setOpacity(0.3);
p.drawRect(100, 100, 200, 200);@Result: http://itmages.ru/image/view/1133748/ed8fbadb
Second way:
@ QPainter p(this);
// widget border
p.drawRect(0, 0, width()-1, height()-1);
p.setBrush(Qt::lightGray);
p.setOpacity(0.3);
p.drawRect(0, 0, 200, 200);
p.setCompositionMode(QPainter::CompositionMode_Clear);
p.drawRect(100, 100, 200, 200);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.drawRect(100, 100, 200, 200);@Result: http://itmages.ru/image/view/1133750/0be2675f
Example shows that after erasing widget get wrong background color of parent widget. In my case it green, but it draws like default system color.
Source code: https://dl.dropboxusercontent.com/u/16019144/PaintExample.zip