<?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; QGraphicsItem</title>
	<atom:link href="http://curtis.humphreyonline.us/tag/qgraphicsitem/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>Qt mousePressEvent and mouseReleaseEvent fun!</title>
		<link>http://curtis.humphreyonline.us/code/qt-code/qt-mousepressevent-and-mousereleaseevent-fun</link>
		<comments>http://curtis.humphreyonline.us/code/qt-code/qt-mousepressevent-and-mousereleaseevent-fun#comments</comments>
		<pubDate>Mon, 02 Mar 2009 18:03:34 +0000</pubDate>
		<dc:creator>Curtis M. Humphrey</dc:creator>
				<category><![CDATA[Qt Code]]></category>
		<category><![CDATA[mousePressEvent]]></category>
		<category><![CDATA[mouseReleaseEvent]]></category>
		<category><![CDATA[QGraphicsItem]]></category>
		<category><![CDATA[Qt FYI]]></category>

		<guid isPermaLink="false">http://curtis.humphreyonline.us/?p=61</guid>
		<description><![CDATA[This would have been good to know beforehand regarding mouse events&#8230;
Apparently in Qt, QGraphicsItem&#8217;s mouse events are a little different from the model I had in my head from Actionscript. In actionscript when a user uses the mouse to move over and then click on an MovieClip object these events occur (ignoring mouse movement events):

onRollOver
onPress
onRelease
onRollOut

In [...]


Related posts:<ol><li><a href='http://curtis.humphreyonline.us/articles/word-2007-spell-checking-inside-tables' rel='bookmark' title='Permanent Link: word 2007 spell checking inside tables'>word 2007 spell checking inside tables</a></li>
<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[<p>This would have been good to know beforehand regarding mouse events&#8230;</p>
<p>Apparently in <a title="Qt" href="http://doc.trolltech.com/">Qt</a>, <a title="QGraphicsItem" href="http://doc.trolltech.com/4.4/qgraphicsitem.html">QGraphicsItem</a>&#8217;s mouse events are a little different from the model I had in my head from <a title="Actionscript" href="http://en.wikipedia.org/wiki/Actionscript">Actionscript</a>. In actionscript when a user uses the mouse to move over and then click on an MovieClip object these events occur (ignoring mouse movement events):</p>
<ol>
<li>onRollOver</li>
<li>onPress</li>
<li>onRelease</li>
<li>onRollOut</li>
</ol>
<p>In Qt it is not quite the same. If the QGraphicsItem accepts mouse clicks but is not <a title="GraphicsItemFlag" href="http://doc.trolltech.com/4.4/qgraphicsitem.html#GraphicsItemFlag-enum" target="_blank">movable or selectable</a> then these are its events (again ignoring mouse movement events):</p>
<ol>
<li>hoverEnterEvent</li>
<li>mousePressEvent</li>
<li>hoverLeaveEvent</li>
</ol>
<p>Notice in Qt there is not a mouse release event called. Qt does have a <a href="http://doc.trolltech.com/4.4/qgraphicsitem.html#mouseReleaseEvent">mouseReleaseEvent</a><strong> </strong>but it is only called if the item is movable or selectable (both not set by default).</p>
<p>This fact would have been good to know. I had been allowing QGraphicsItems to move during debuging and had been using mouseReleaseEvent as my click event. When I removed the debuging code suddenly I could not click my items, which was most distressing as it was only minutes away from my live demo. Luckly, I discovered this Qt fact, changed the event to  <a href="http://doc.trolltech.com/4.4/qgraphicsitem.html#mousePressEvent">mousePressEvent</a> and got it all working.</p>
<p>How demos work as told on <a href="http://www.phdcomics.com/comics/archive.php?comicid=821">PHD comics</a>.</p>


<p>Related posts:<ol><li><a href='http://curtis.humphreyonline.us/articles/word-2007-spell-checking-inside-tables' rel='bookmark' title='Permanent Link: word 2007 spell checking inside tables'>word 2007 spell checking inside tables</a></li>
<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/qt-mousepressevent-and-mousereleaseevent-fun/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
