<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>In Search of a Good Title &#187; QPainter</title>
	<atom:link href="http://curtis.humphreyonline.us/tag/qpainter/feed" rel="self" type="application/rss+xml" />
	<link>http://curtis.humphreyonline.us</link>
	<description>Curtis M. Humphrey's Website</description>
	<lastBuildDate>Wed, 02 Jun 2010 17:04:44 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making a QPixmap transparent the Right Way</title>
		<link>http://curtis.humphreyonline.us/code/qt-code/making-a-qpixmap-transparent-wrong-way-right-way</link>
		<comments>http://curtis.humphreyonline.us/code/qt-code/making-a-qpixmap-transparent-wrong-way-right-way#comments</comments>
		<pubDate>Sat, 21 Feb 2009 22:55:42 +0000</pubDate>
		<dc:creator>Curtis M. Humphrey</dc:creator>
				<category><![CDATA[Qt Code]]></category>
		<category><![CDATA[QPainter]]></category>
		<category><![CDATA[QPixmap]]></category>
		<category><![CDATA[Qt 4.4]]></category>
		<category><![CDATA[Understanding Qt]]></category>

		<guid isPermaLink="false">http://curtis.humphreyonline.us/?p=45</guid>
		<description><![CDATA[The Problem
The classic situation that this problem can occur is when one wants to create a QPixmap with a transparent background and then draws on only some of the available pixels. This process is akin to creating a transparent GIF. Well there is a wrong way and a right way. I discovered this because I [...]


Related posts:<ol><li><a href='http://curtis.humphreyonline.us/code/qt-code/grabbing-all-qgraphicitems-current-viewable-in-a-qgraphicview' rel='bookmark' title='Permanent Link: Grabbing all QGraphicItems current viewable in a QGraphicView'>Grabbing all QGraphicItems current viewable in a QGraphicView</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>The Problem</h2>
<p>The classic situation that this problem can occur is when one wants to create a QPixmap with a transparent background and then draws on only some of the available pixels. This process is akin to creating a transparent GIF. Well there is a wrong way and a right way. I discovered this because I was using the wrong way and was getting away with it as long as I was writing to every pixel. Then there was a case when I was not writing to all pixels and the QPixmap unwritten to pixels (i.e., the background) was not transparent as I expected. Here is the wrong way.</p>
<h3>The Wrong Way</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #22aadd;">QPixmap</span> <span style="color: #006E28;">*</span>pix <span style="color: #006E28;">=</span> <span style="color: #000000; font-weight:bold;">new</span> <span style="color: #22aadd;">QPixmap</span><span style="color: #006E28;">&#40;</span>Size<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
<span style="color: #22aadd;">QPainter</span> painter<span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">begin</span><span style="color: #006E28;">&#40;</span>pix<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">fillRect</span><span style="color: #006E28;">&#40;</span><span style="color: #B08000;">0</span><span style="color: #006E28;">,</span><span style="color: #B08000;">0</span><span style="color: #006E28;">,</span>pix<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">width</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">,</span>pix<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">height</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">,</span><span style="color: #22aadd;">QBrush</span><span style="color: #006E28;">&#40;</span><span style="color: #22aadd;">QColor</span><span style="color: #006E28;">&#40;</span><span style="color: #B08000;">0</span><span style="color: #006E28;">,</span><span style="color: #B08000;">0</span><span style="color: #006E28;">,</span><span style="color: #B08000;">0</span><span style="color: #006E28;">,</span><span style="color: #B08000;">0</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">drawPixmap</span><span style="color: #006E28;">&#40;</span>pix<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">rect</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">,*</span>m_image<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">end</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span></pre></td></tr></table></div>

<p>The problem is that line 4 does not fill the QPixmap with anything as the alpha is set to 0. Think of fill with alpha as a merge operation and not a replace. I was thinking of it as a replace and therefore I was replacing every pixel with a fully transparent black. However, because fillRect is really a merge I was merging a transparent pixel with the background effectively doing nothing.</p>
<p>The correct way is as follows:</p>
<h3>The Right Way</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #22aadd;">QPixmap</span> <span style="color: #006E28;">*</span>pix <span style="color: #006E28;">=</span> <span style="color: #000000; font-weight:bold;">new</span> <span style="color: #22aadd;">QPixmap</span><span style="color: #006E28;">&#40;</span>Size<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
pix<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">fill</span><span style="color: #006E28;">&#40;</span>Qt<span style="color: #006E28;">::</span><span style="color: #2B74C7;">transparent</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
<span style="color: #22aadd;">QPainter</span> painter<span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">begin</span><span style="color: #006E28;">&#40;</span>pix<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">drawPixmap</span><span style="color: #006E28;">&#40;</span>pix<span style="color: #006E28;">-&gt;;</span>rect<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">,*</span>m_baseSymbol<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
painter.<span style="color: #2B74C7;">end</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span></pre></td></tr></table></div>

<p>Notice that line 2 is new and this fill command is a replace fill.</p>
<h2>Summary</h2>
<p>In summary QPixmap fill(&#8230;) command is a replace fill; whereas, QPainter fillRect(&#8230;) is a merge fill. I hope this help clarity the nature of these two operations.</p>


<p>Related posts:<ol><li><a href='http://curtis.humphreyonline.us/code/qt-code/grabbing-all-qgraphicitems-current-viewable-in-a-qgraphicview' rel='bookmark' title='Permanent Link: Grabbing all QGraphicItems current viewable in a QGraphicView'>Grabbing all QGraphicItems current viewable in a QGraphicView</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://curtis.humphreyonline.us/code/qt-code/making-a-qpixmap-transparent-wrong-way-right-way/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
