<?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; Qt 4.4</title>
	<atom:link href="http://curtis.humphreyonline.us/tag/qt-4_4/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>Grabbing all QGraphicItems current viewable in a QGraphicView</title>
		<link>http://curtis.humphreyonline.us/code/qt-code/grabbing-all-qgraphicitems-current-viewable-in-a-qgraphicview</link>
		<comments>http://curtis.humphreyonline.us/code/qt-code/grabbing-all-qgraphicitems-current-viewable-in-a-qgraphicview#comments</comments>
		<pubDate>Tue, 07 Apr 2009 18:14:09 +0000</pubDate>
		<dc:creator>Curtis M. Humphrey</dc:creator>
				<category><![CDATA[Qt Code]]></category>
		<category><![CDATA[QGraphicsItem]]></category>
		<category><![CDATA[QGraphicsView]]></category>
		<category><![CDATA[Qt 4.4]]></category>
		<category><![CDATA[Qt FYI]]></category>

		<guid isPermaLink="false">http://curtis.humphreyonline.us/?p=94</guid>
		<description><![CDATA[The best way to get a list of all items current displayed inside the QGraphicsView is done through the combination of three steps:

get viewing rectangle
get items intersecting or inside rectangle
check for visibility



1
2
3
4
5
6
7
QList&#60;QGraphicsItem* &#62; viewItems = m_pView-&#62;items&#40;m_pView-&#62;rect&#40;&#41;&#41;;
&#160;
for&#40;int i=0; i &#60; viewItems.size&#40;&#41;; ++i&#41; &#123;
	if&#40;viewItems.at&#40;i&#41;-&#62;isVisible&#40;&#41;&#41; &#123;
		//payload
	&#125;
&#125;

Do not use other rectangle functions like sceneRect(), childrenRect(), contentsRect(), etc&#8230; as they [...]


Related posts:<ol><li><a href='http://curtis.humphreyonline.us/code/qt-code/making-a-qpixmap-transparent-wrong-way-right-way' rel='bookmark' title='Permanent Link: Making a QPixmap transparent the Right Way'>Making a QPixmap transparent the Right Way</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The best way to get a list of all items current displayed inside the QGraphicsView is done through the combination of three steps:</p>
<ol>
<li>get viewing rectangle</li>
<li>get items intersecting or inside rectangle</li>
<li>check for visibility</li>
</ol>
<ol></ol>

<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;">QList</span><span style="color: #006E28;">&lt;</span><span style="color: #22aadd;">QGraphicsItem</span><span style="color: #006E28;">*</span> <span style="color: #006E28;">&gt;</span> viewItems <span style="color: #006E28;">=</span> m_pView<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">items</span><span style="color: #006E28;">&#40;</span>m_pView<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;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
<span style="color: #000000; font-weight:bold;">for</span><span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> i<span style="color: #006E28;">=</span><span style="color: #B08000;">0</span><span style="color: #006E28;">;</span> i <span style="color: #006E28;">&lt;</span> viewItems.<span style="color: #2B74C7;">size</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span> <span style="color: #006E28;">++</span>i<span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
	<span style="color: #000000; font-weight:bold;">if</span><span style="color: #006E28;">&#40;</span>viewItems.<span style="color: #2B74C7;">at</span><span style="color: #006E28;">&#40;</span>i<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">isVisible</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
		<span style="color: #888888;">//payload</span>
	<span style="color: #006E28;">&#125;</span>
<span style="color: #006E28;">&#125;</span></pre></td></tr></table></div>

<p>Do not use other rectangle functions like sceneRect(), childrenRect(), contentsRect(), etc&#8230; as they will not give you the correct bounding region. Also this operation returns all items, so one must check for visibility manually and cast points to any derived class as appropriate.</p>
<p>With casting</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #22aadd;">QList</span><span style="color: #006E28;">&lt;</span><span style="color: #22aadd;">QGraphicsItem</span><span style="color: #006E28;">*</span> <span style="color: #006E28;">&gt;</span> viewItems <span style="color: #006E28;">=</span> m_pView<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">items</span><span style="color: #006E28;">&#40;</span>m_pView<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;">&#41;</span><span style="color: #006E28;">;</span>
NewType <span style="color: #006E28;">*</span>newType<span style="color: #006E28;">;</span>
&nbsp;
<span style="color: #000000; font-weight:bold;">for</span><span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> i<span style="color: #006E28;">=</span><span style="color: #B08000;">0</span><span style="color: #006E28;">;</span> i <span style="color: #006E28;">&lt;</span> viewItems.<span style="color: #2B74C7;">size</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span> <span style="color: #006E28;">++</span>i<span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
	<span style="color: #000000; font-weight:bold;">if</span><span style="color: #006E28;">&#40;</span>viewItems.<span style="color: #2B74C7;">at</span><span style="color: #006E28;">&#40;</span>i<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">isVisible</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
		newType <span style="color: #006E28;">=</span> <span style="color: #0057AE;">dynamic_cast</span><span style="color: #006E28;">&lt;</span>NewType <span style="color: #006E28;">*&gt;</span> <span style="color: #006E28;">&#40;</span>viewItems<span style="color: #006E28;">&#91;</span>i<span style="color: #006E28;">&#93;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
		<span style="color: #000000; font-weight:bold;">if</span><span style="color: #006E28;">&#40;</span>newType <span style="color: #006E28;">!=</span> <span style="color: #0057AE;">NULL</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
			<span style="color: #888888;">//payload</span>
		<span style="color: #006E28;">&#125;</span>
	<span style="color: #006E28;">&#125;</span>
<span style="color: #006E28;">&#125;</span></pre></td></tr></table></div>



<p>Related posts:<ol><li><a href='http://curtis.humphreyonline.us/code/qt-code/making-a-qpixmap-transparent-wrong-way-right-way' rel='bookmark' title='Permanent Link: Making a QPixmap transparent the Right Way'>Making a QPixmap transparent the Right Way</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://curtis.humphreyonline.us/code/qt-code/grabbing-all-qgraphicitems-current-viewable-in-a-qgraphicview/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
