<?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; QGraphicsView</title>
	<atom:link href="http://curtis.humphreyonline.us/tag/qgraphicsview/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>
	</channel>
</rss>
