<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problem with erasing of rectangle area in paintEvent]]></title><description><![CDATA[<p dir="auto">I need to draw two overlapping rects. Both with transparent.<br />
Some thing like this: <a href="http://itmages.ru/image/view/1133740/8fdac9c4" target="_blank" rel="noopener noreferrer nofollow ugc">http://itmages.ru/image/view/1133740/8fdac9c4</a><br />
So we have one rect with transparent under other without transparent, then we group it and set transparent to group (example made in Inkscape).</p>
<p dir="auto">I tries to make it in qt, but have some issues when I paint it in widget which over parent.</p>
<p dir="auto">First way:<br />
@    QPainter p(this);<br />
// widget border<br />
p.drawRect(0, 0, width()-1, height()-1);<br />
p.setBrush(Qt::lightGray);<br />
p.setOpacity(0.3);<br />
p.drawRect(0, 0, 200, 200);<br />
p.setOpacity(1);<br />
p.eraseRect(100, 100, 200, 200);<br />
p.setOpacity(0.3);<br />
p.drawRect(100, 100, 200, 200);@</p>
<p dir="auto">Result: <a href="http://itmages.ru/image/view/1133748/ed8fbadb" target="_blank" rel="noopener noreferrer nofollow ugc">http://itmages.ru/image/view/1133748/ed8fbadb</a></p>
<p dir="auto">Second way:<br />
@    QPainter p(this);<br />
// widget border<br />
p.drawRect(0, 0, width()-1, height()-1);<br />
p.setBrush(Qt::lightGray);<br />
p.setOpacity(0.3);<br />
p.drawRect(0, 0, 200, 200);<br />
p.setCompositionMode(QPainter::CompositionMode_Clear);<br />
p.drawRect(100, 100, 200, 200);<br />
p.setCompositionMode(QPainter::CompositionMode_SourceOver);<br />
p.drawRect(100, 100, 200, 200);@</p>
<p dir="auto">Result: <a href="http://itmages.ru/image/view/1133750/0be2675f" target="_blank" rel="noopener noreferrer nofollow ugc">http://itmages.ru/image/view/1133750/0be2675f</a></p>
<p dir="auto">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.</p>
<p dir="auto">Source code: <a href="https://dl.dropboxusercontent.com/u/16019144/PaintExample.zip" target="_blank" rel="noopener noreferrer nofollow ugc">https://dl.dropboxusercontent.com/u/16019144/PaintExample.zip</a></p>
]]></description><link>https://forum.qt.io/topic/29817/problem-with-erasing-of-rectangle-area-in-paintevent</link><generator>RSS for Node</generator><lastBuildDate>Fri, 19 Jun 2026 08:54:17 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/29817.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 23 Jul 2013 19:15:59 GMT</pubDate><ttl>60</ttl></channel></rss>