<?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>Linux Tech Blog</title>
	<atom:link href="http://fredyduarte.net/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://fredyduarte.net/blog</link>
	<description>Frederico Duarte</description>
	<lastBuildDate>Tue, 22 Jun 2010 22:55:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Announcing QFacebook: Qt port of Facebook Graph API</title>
		<link>http://fredyduarte.net/blog/?p=332</link>
		<comments>http://fredyduarte.net/blog/?p=332#comments</comments>
		<pubDate>Tue, 22 Jun 2010 22:55:06 +0000</pubDate>
		<dc:creator>fred</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[Qt]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=332</guid>
		<description><![CDATA[Hi, everyone. Sorry, but no coding examples for today (even though I haven&#8217;t posted anything in a while  ).
Today, I&#8217;m announcing a new project open for development: QFacebook. It consists of a Qt port of the Facebook Graph API.
The interest in creating this project came from another one that we are currently working on [...]]]></description>
			<content:encoded><![CDATA[<p>Hi, everyone. Sorry, but no coding examples for today (even though I haven&#8217;t posted anything in a while <img src='http://fredyduarte.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p>Today, I&#8217;m announcing a new project open for development: <strong>QFacebook</strong>. It consists of a Qt port of the Facebook <a href="http://developers.facebook.com/docs/api">Graph API</a>.</p>
<p>The interest in creating this project came from another one that we are currently working on at <a href="http://www.indt.org/en.html">INDT</a>. We needed to access and manage Facebook accounts, but we didn&#8217;t want to use the <em><a href="http://developers.facebook.com/docs/reference/rest/">old rest API</a></em>.</p>
<p>There are three non-C++ solutions available: the official <a href="http://github.com/facebook/connect-js/">Javascript SDK</a> from Facebook, a <a href="http://github.com/facebook/php-sdk/">PHP SDK</a> and a <a href="http://github.com/facebook/python-sdk">Python SDK</a>. Seeing the lack of a <a href="http://qt.nokia.com/">Qt</a> port for this API we decided to make our own library.</p>
<p>The project is hosted at gitorious and the the URL is:</p>
<ul>
<li><strong><a title="QFacebook" href="http://gitorious.org/qfacebook">http://gitorious.org/qfacebook</a></strong></li>
</ul>
<p>As of now, the project has two usage examples that you can start coding with. But we are still working on the documentation.</p>
<p>Anyone is welcome use it or even join our development effort.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=332</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling GraphicsView events &#8211; Part. 2</title>
		<link>http://fredyduarte.net/blog/?p=299</link>
		<comments>http://fredyduarte.net/blog/?p=299#comments</comments>
		<pubDate>Wed, 09 Dec 2009 22:40:56 +0000</pubDate>
		<dc:creator>fred</dc:creator>
				<category><![CDATA[GraphicsView]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Widgets]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=299</guid>
		<description><![CDATA[In my previous post, I&#8217;ve shown you how to handle events (specifically mouse events) inside GraphicsView.
Lets expand this idea and create a customized widget on Graphics View. The simplest widget of them all is the Push Button (actually the simplest widget is the Label, but then, I wouldn&#8217;t have much to write on this post [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I&#8217;ve shown you how to handle events (specifically mouse events) inside <strong>GraphicsView</strong>.</p>
<p>Lets expand this idea and create a customized widget on <strong>Graphics View</strong>. The simplest widget of them all is the <a href="http://doc.trolltech.com/4.5/qpushbutton.html">Push Button</a> (actually the simplest widget is the <a href="http://doc.trolltech.com/4.5/qlabel.html">Label</a>, but then, I wouldn&#8217;t have much to write on this post <img src='http://fredyduarte.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p>The Button widget has the following characteristics:</p>
<ol>
<li>Display 2 images depending on its state (<em>normal</em> &amp; <em>pressed</em>);</li>
<li>Detects mouse clicks (including moving the mouse while clicked);</li>
</ol>
<p>On to the coding part&#8230;</p>
<p>There&#8217;s only one attribute for storing the Button&#8217;s state: <em>m_isPressed</em>. Depending on the value of <em>m_isPressed</em>, the <strong>paint()</strong> method will either draw one of two <a href="http://doc.trolltech.com/4.5/qpixmap.html"><strong>QPixmap</strong></a> objects, <em>m_normal</em> or <em>m_pressed</em>.</p>
<p>Here&#8217;s the <strong>paint()</strong> method for the <strong>Button</strong> class:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #577a61;color: #343832;">void</span> Button<span style="color: #008080;">::</span><span style="color: #007788;">paint</span><span style="color: #008000;color: #CCC;">&#40;</span>QPainter <span style="color: #ffff00;">*</span>painter,
                   <span style="color: #577a61;color: #343832;">const</span> QStyleOptionGraphicsItem <span style="color: #ffff00;">*</span>option,
                   QWidget <span style="color: #ffff00;">*</span>widget<span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
    Q_UNUSED<span style="color: #008000;color: #CCC;">&#40;</span>option<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    Q_UNUSED<span style="color: #008000;color: #CCC;">&#40;</span>widget<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>m_isPressed<span style="color: #008000;color: #CCC;">&#41;</span>
        painter<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>drawPixmap<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, m_pressed<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;color: #B83A24;">else</span>
        painter<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>drawPixmap<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, m_normal<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></div></div>

<p>The change between the normal and pressed image happens through the mouse handling methods. Here&#8217;s the <strong>mousePressEvent()</strong> method:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #577a61;color: #343832;">void</span> Button<span style="color: #008080;">::</span><span style="color: #007788;">mousePressEvent</span><span style="color: #008000;color: #CCC;">&#40;</span>QGraphicsSceneMouseEvent <span style="color: #ffff00;">*</span>event<span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
    <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>button<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #ffffff;">==</span> Qt<span style="color: #008080;">::</span><span style="color: #007788;">LeftButton</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #008000;color: #CCC;">&#123;</span>
        <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>contains<span style="color: #008000;color: #CCC;">&#40;</span>event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>pos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span>
            m_isPressed <span style="color: #ffffff;">=</span> <span style="color: #0000ff;color: #577A61;">true</span><span style="color: #008080;">;</span>
&nbsp;
        update<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;color: #CCC;">&#125;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></div></div>

<p>If the user clicks within the image area, <em>m_isPressed</em> is set. Therefore, the pressed image is displayed.</p>
<p>Now the <strong>mouseMoveEvent()</strong> method:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #577a61;color: #343832;">void</span> Button<span style="color: #008080;">::</span><span style="color: #007788;">mouseMoveEvent</span><span style="color: #008000;color: #CCC;">&#40;</span>QGraphicsSceneMouseEvent <span style="color: #ffff00;">*</span>event<span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
    <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>buttons<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #00ff00;">&amp;</span> Qt<span style="color: #008080;">::</span><span style="color: #007788;">LeftButton</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #008000;color: #CCC;">&#123;</span>
        <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>contains<span style="color: #008000;color: #CCC;">&#40;</span>event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>pos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #008000;color: #CCC;">&#123;</span>
            m_isPressed <span style="color: #ffffff;">=</span> <span style="color: #0000ff;color: #577A61;">true</span><span style="color: #008080;">;</span>
        <span style="color: #008000;color: #CCC;">&#125;</span> <span style="color: #0000ff;color: #B83A24;">else</span> <span style="color: #008000;color: #CCC;">&#123;</span>
            m_isPressed <span style="color: #ffffff;">=</span> <span style="color: #0000ff;color: #577A61;">false</span><span style="color: #008080;">;</span>
        <span style="color: #008000;color: #CCC;">&#125;</span>
&nbsp;
        update<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;color: #CCC;">&#125;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></div></div>

<p>It the user moves the mouse (while clicking) to outside the image area, <em>m_isPressed</em> is changed.</p>
<p>And finally, the <strong>mouseReleaseEvent()</strong> method:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #577a61;color: #343832;">void</span> Button<span style="color: #008080;">::</span><span style="color: #007788;">mouseReleaseEvent</span><span style="color: #008000;color: #CCC;">&#40;</span>QGraphicsSceneMouseEvent <span style="color: #ffff00;">*</span>event<span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
    <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>button<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #ffffff;">==</span> Qt<span style="color: #008080;">::</span><span style="color: #007788;">LeftButton</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #008000;color: #CCC;">&#123;</span>
        m_isPressed <span style="color: #ffffff;">=</span> <span style="color: #0000ff;color: #577A61;">false</span><span style="color: #008080;">;</span>
&nbsp;
        update<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;color: #B83A24;">if</span> <span style="color: #008000;color: #CCC;">&#40;</span>contains<span style="color: #008000;color: #CCC;">&#40;</span>event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>pos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #008000;color: #CCC;">&#123;</span>
            emit clicked<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;color: #CCC;">&#125;</span>
    <span style="color: #008000;color: #CCC;">&#125;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></div></div>

<p>The <em>m_isPressed</em> attribute is unset and the <em>clicked()</em> signal is emitted (if the mouse stays within the image area).<br />
Here&#8217;s the customized Button in action:</p>
<p><img class="aligncenter" src="http://lh5.ggpht.com/_JvrQBTmjIS4/SyAJ__wBiWI/AAAAAAAAAEU/1RqyQDRV8aE/button-click.gif" alt="" width="250" height="250" /></p>
<p>The source code for this example is at my gitorious <strong><a href="http://gitorious.org/tech-blog/sources">repository</a></strong>, under the <strong>button</strong> dir:</p>
<p><a href="http://gitorious.org/tech-blog/sources/trees/master/button">http://gitorious.org/tech-blog/sources/trees/master/button</a></p>
<p>Because the image is a rounded one, there are some parts &#8220;outside&#8221; of the Button that also receive mouse events. This happens because we still need to determine the &#8220;<em>clickable</em>&#8221; area through the <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#shape"><strong>shape()</strong></a> method. But that&#8217;s something for another post. <img src='http://fredyduarte.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Till next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=299</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Handling GraphicsView events &#8211; Part. 1</title>
		<link>http://fredyduarte.net/blog/?p=261</link>
		<comments>http://fredyduarte.net/blog/?p=261#comments</comments>
		<pubDate>Mon, 16 Nov 2009 15:42:55 +0000</pubDate>
		<dc:creator>fred</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[GraphicsView]]></category>
		<category><![CDATA[Qt]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=261</guid>
		<description><![CDATA[Hi, everyone. It&#8217;s been while since I&#8217;ve posted something here.
To take out some of the dust from the blog  ,  I figured we could start with something simple like handling events on the GraphicsView framework.
It works like this: The view (QGraphicsView) receives events and passes them to the scene (QGraphicsScene). In order to pass [...]]]></description>
			<content:encoded><![CDATA[<p>Hi, everyone. It&#8217;s been while since I&#8217;ve posted something here.</p>
<p>To take out some of the dust from the blog <img src='http://fredyduarte.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ,  I figured we could start with something simple like handling events on the GraphicsView framework.</p>
<p>It works like this: The view (<strong>QGraphicsView</strong>) receives events and passes them to the scene (<strong>QGraphicsScene</strong>). In order to pass these events to the scene, the view must first convert these units (pixel coordinates) into scene units (vertex coordinates). This conversion process is transparent, but you access these coordinates through convenience methods like <a href="http://doc.trolltech.com/4.5/qgraphicsview.html#mapFromScene">mapFromScene</a> and <a href="http://doc.trolltech.com/4.5/qgraphicsview.html#mapToScene">mapToScene</a>. This also applies for the inverse path (passing events from the scene to the view).</p>
<p>Now, let&#8217;s say you want to capture mouse events. To do that, you have to reimplement the <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#mousePressEvent">mousePressEvent</a> and/or <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#mouseReleaseEvent">mouseReleaseEvent</a> methods. Depending on your needs, they can be reimplemented from within the <strong>QGraphicsItem</strong> class (handling single items) or the <strong>QGraphicsScene</strong> class (handling the whole scene).</p>
<p>Here&#8217;s an example (I&#8217;ve used the class from the previous post as reference):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #577a61;color: #343832;">void</span> CustomItem<span style="color: #008080;">::</span><span style="color: #007788;">mousePressEvent</span><span style="color: #008000;color: #CCC;">&#40;</span>QGraphicsSceneMouseEvent <span style="color: #ffff00;">*</span>event<span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
  qDebug<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #ffffff;">&lt;&lt;</span> <span style="color: #FF0000;color: #666666;">&quot;Mouse button clicked at position: &quot;</span>
           <span style="color: #ffffff;">&lt;&lt;</span> event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>pos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;color: #CCC;">&#125;</span>
&nbsp;
<span style="color: #577a61;color: #343832;">void</span> CustomItem<span style="color: #008080;">::</span><span style="color: #007788;">mouseReleaseEvent</span><span style="color: #008000;color: #CCC;">&#40;</span>QGraphicsSceneMouseEvent <span style="color: #ffff00;">*</span>event<span style="color: #008000;color: #CCC;">&#41;</span>
<span style="color: #008000;color: #CCC;">&#123;</span>
  qDebug<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #ffffff;">&lt;&lt;</span> <span style="color: #FF0000;color: #666666;">&quot;Mouse button released at position: &quot;</span>
           <span style="color: #ffffff;">&lt;&lt;</span> event<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>pos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;color: #CCC;">&#125;</span></pre></div></div>

<p>Every time you click on the item, it will display a <em>clicked</em> message, and every time you release the mouse, it will display a <em>released</em> message. These methods are called whenever the desired event happens (Mouse presses, Mouse releases, Mouse moves, Key presses, Key releases, etc).</p>
<p>The <a href="http://doc.trolltech.com/4.5/qgraphicsscenemouseevent.html">QGraphicsSceneMouseEvent</a> is the mouse event object. It&#8217;s a similar class to the <a href="http://doc.trolltech.com/4.5/qmouseevent.html">QMouseEvent</a> class, except that it only handles the <strong>QGraphicsView</strong> mouse events and not <strong>QWidget</strong> events.</p>
<p>There are also other handling methods like: <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#mouseMoveEvent">mouseMoveEvent</a> and <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#mouseDoubleClickEvent">mouseDoubleClickEvent</a> for mouse events or <a href="http://doc.trolltech.com/4.5/qgraphicsscene.html#keyPressEvent">keyPressEvent</a> and <a href="http://doc.trolltech.com/4.5/qgraphicsscene.html#keyReleaseEvent">keyReleaseEvent</a> for keyboard events. For the keyboard ones, the item must be able to receive keyboard focus (<strong>QGraphicsItem::ItemIsFocusable</strong> flag set). You just need to reimplement them as in the code above.</p>
<p>And that&#8217;s it. I&#8217;ve also moved my previous examples (plus this one) to a repository at the <a href="http://qt.gitorious.org/">Qt Gitorious</a>.</p>
<p>The URL is <a href="http://gitorious.org/tech-blog/sources">http://gitorious.org/tech-blog/sources</a>.</p>
<p>The example from this post is under the <a href="http://gitorious.org/tech-blog/sources/trees/master/handling-events">handling-events</a> dir.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=261</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating custom QGraphicsItems</title>
		<link>http://fredyduarte.net/blog/?p=208</link>
		<comments>http://fredyduarte.net/blog/?p=208#comments</comments>
		<pubDate>Sat, 13 Jun 2009 17:49:18 +0000</pubDate>
		<dc:creator>fred</dc:creator>
				<category><![CDATA[Qt]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=208</guid>
		<description><![CDATA[Previously, I&#8217;ve shown you how to place items in a scene (through convenience functions). Now we&#8217;ll talk about how to create your own custom QGraphicsItems.
In order to do that, you must first subclass from QGraphicsItem class. There are two important methods to be reimplemented:

boundingRect: virtual function that returns the area that needs to be painted. [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, I&#8217;ve shown you how to place items in a scene (through convenience functions). Now we&#8217;ll talk about how to create your own custom <strong>QGraphicsItems</strong>.</p>
<p>In order to do that, you must first subclass from <strong>QGraphicsItem</strong> class. There are two important methods to be reimplemented:</p>
<ul>
<li><strong><a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#boundingRect">boundingRect</a></strong>: virtual function that returns the area that needs to be painted. All items have a bounding rectangle and <em>QGraphicsView</em> uses this function to determine what parts need to be redrawn.</li>
<li><strong><a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#paint">paint</a></strong>: virtual function that implements the painting method itself. It uses a <em>QPainter</em> object.</li>
</ul>
<p>Let&#8217;s take a look at a sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;"><span style="color: #0000ff;color: #577A61;">class</span> CustomItem <span style="color: #008080;">:</span> <span style="color: #0000ff;color: #577A61;">public</span> QGraphicsItem
<span style="color: #008000;color: #CCC;">&#123;</span>
<span style="color: #0000ff;color: #577A61;">public</span><span style="color: #008080;">:</span>
    CustomItem<span style="color: #008000;color: #CCC;">&#40;</span>QGraphicsItem <span style="color: #ffff00;">*</span>parent <span style="color: #ffffff;">=</span> <span style="color: #0000dd;color: #DDD;">0</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    QRectF boundingRect<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #577a61;color: #343832;">const</span>
    <span style="color: #008000;color: #CCC;">&#123;</span>
        <span style="color: #0000ff;color: #B83A24;">return</span> QRectF<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">250</span>, <span style="color: #0000dd;color: #DDD;">250</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;color: #CCC;">&#125;</span>
&nbsp;
    <span style="color: #577a61;color: #343832;">void</span> paint<span style="color: #008000;color: #CCC;">&#40;</span>QPainter <span style="color: #ffff00;">*</span>painter,
               <span style="color: #577a61;color: #343832;">const</span> QStyleOptionGraphicsItem <span style="color: #ffff00;">*</span>option,
               QWidget <span style="color: #ffff00;">*</span>widget<span style="color: #008000;color: #CCC;">&#41;</span>
    <span style="color: #008000;color: #CCC;">&#123;</span>
        painter<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setRenderHint<span style="color: #008000;color: #CCC;">&#40;</span>QPainter<span style="color: #008080;">::</span><span style="color: #007788;">Antialiasing</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
        painter<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setBrush<span style="color: #008000;color: #CCC;">&#40;</span>Qt<span style="color: #008080;">::</span><span style="color: #007788;">blue</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
        painter<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>drawRoundedRect<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">250</span>, <span style="color: #0000dd;color: #DDD;">250</span>, <span style="color: #0000dd;color: #DDD;">5</span>, <span style="color: #0000dd;color: #DDD;">5</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;color: #CCC;">&#125;</span>
<span style="color: #008000;color: #CCC;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>As you can see, the <strong>boundingRect</strong> method returns a <a href="http://doc.trolltech.com/4.5/qrectf.html">QRectF</a> object containing the draw area. Inside the <strong>paint</strong> method, we set the brush color to <span style="color: #3366ff;">blue</span> and draw a rounded rectangle of same size as the bounding rectangle. Also, the <span style="text-decoration: underline;">Antialiasing</span> flag is enabled so that the engine draw smooth edges.</p>
<p>There are 2 more parameters in the <strong>paint</strong> method: <em>QStyleOptionGraphicsItem</em> and <em>QWidget</em>. The first is the style option for the item and the second is an optional parameter that describes the widget to draw the item.</p>
<p>The <em>QPainter</em> parameter is the most important one right now. But, as you become more acquainted with the Qt Canvas Programming, you should look further into the other 2 parameters, specially the <em>QStyleOptionGraphicsItem</em>.</p>
<p>After adding the item (<strong>CustomItem</strong> class) to the scene and displaying it, the result is this:</p>
<p style="text-align: center;">
<div id="attachment_240" class="wp-caption aligncenter" style="width: 190px"><img class="size-full wp-image-240" title="custom-item01" src="http://fredyduarte.net/blog/wp-content/uploads/2009/06/custom-item01.png" alt="Custom Item drawn using the pain and boundingRect methods." width="180" height="181" /><p class="wp-caption-text">Custom item drawn from QGraphicsItem.</p></div>
<p>There are several options to choose from when drawing an item with the <em>QPainter</em> object. You just have to replace the <span style="text-decoration: underline;">drawRoundedRect</span> method with the another suitable one. There are a couple of examples below:</p>
<p style="text-align: center;">
<div id="attachment_248" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-248" title="custom-item-examples" src="http://fredyduarte.net/blog/wp-content/uploads/2009/06/custom-item-examples.png" alt="QPainter draw methods." width="450" height="326" /><p class="wp-caption-text">QPainter draw methods.</p></div>
<p>The source code used for this post is available here: <a href="http://fredyduarte.net/blog/uploads/custom-qgraphicsitem.tar.gz">custom-qgraphicsitem.tar.gz</a>. Use it to create your customized <strong>QGraphicsItems</strong>.</p>
<p>Till next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=208</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>QGraphicsItems in a Scene</title>
		<link>http://fredyduarte.net/blog/?p=110</link>
		<comments>http://fredyduarte.net/blog/?p=110#comments</comments>
		<pubDate>Tue, 26 May 2009 20:15:02 +0000</pubDate>
		<dc:creator>fred</dc:creator>
				<category><![CDATA[Qt]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=110</guid>
		<description><![CDATA[Last time, I gave an introduction to the QGraphicsView Framework. Now, I&#8217;ll show you how to create QGraphicsItems in a scene.
First, you need to understand the Coordinate System. There are 3 coordinate systems in use with the framework: View coordinates, Scene coordinates and Item coordinates.

The View coordinate system: represents the QGraphicsView widget coordinates. Each unit [...]]]></description>
			<content:encoded><![CDATA[<p>Last time, I gave an introduction to the QGraphicsView Framework. Now, I&#8217;ll show you how to create QGraphicsItems in a scene.</p>
<p>First, you need to understand the Coordinate System. There are 3 coordinate systems in use with the framework: View coordinates, Scene coordinates and Item coordinates.</p>
<ol>
<li><strong>The View coordinate system</strong>: represents the QGraphicsView widget coordinates. Each unit in this system corresponds to one pixel on the screen.</li>
<li><strong>The Scene coordinate system</strong>: represents logical units of a QGraphicsScene. Top-level items on the scene are positioned using this system before mapping to the View.</li>
<li><strong>The Item coordinate system</strong>: represents the QGraphicsItem local coordinates. These coordinates are centered (by default) around the Item&#8217;s <span style="text-decoration: underline;">(0, 0)</span> point.</li>
</ol>
<p>Two example scenarios are demonstrated below:</p>
<div id="attachment_131" class="wp-caption aligncenter" style="width: 528px"><img class="size-full wp-image-131" title="Coordinate System" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/coordinate-system2.png" alt="coordinate-system2" width="518" height="260" /><p class="wp-caption-text">LEFT: Untransformed View. RIGHT: Zoomed Out View. Each pixel on the View correspond to 4 logical units on the Scene. (This is only a demonstrative example)</p></div>
<p style="text-align: left;">The picture on the <span style="color: #ff0000;">LEFT</span> shows a sample scene (QGraphicsScene) visualized by an untransformed view (QGraphicsView). The picture on the <span style="color: #3366ff;">RIGHT</span> shows the same scene, but the view is changed. It has a ZOOM OUT operation applied to it.</p>
<p style="text-align: left;">Notice that each logical unit on the <span style="color: #3366ff;">RIGHT</span> scene does not represent a pixel on the screen anymore. However, each item remains with the SAME coordinates and <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#boundingRect">bounding rectangles</a>.</p>
<p style="text-align: left;">Now, let&#8217;s do some coding. First, we add a rectangle and a circle to the scene:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;">QGraphicsScene scene<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">240</span>, <span style="color: #0000dd;color: #DDD;">120</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
QGraphicsRectItem <span style="color: #ffff00;">*</span>rect<span style="color: #008080;">;</span>
rect <span style="color: #ffffff;">=</span> <span style="color: #0000dd;color: #8FB394;">new</span> QGraphicsRectItem<span style="color: #008000;color: #CCC;">&#40;</span>QRect<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">100</span>, <span style="color: #0000dd;color: #DDD;">100</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
rect<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setBrush<span style="color: #008000;color: #CCC;">&#40;</span> Qt<span style="color: #008080;">::</span><span style="color: #007788;">red</span> <span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
rect<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setPos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">10</span>, <span style="color: #0000dd;color: #DDD;">10</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
scene.<span style="color: #007788;">addItem</span><span style="color: #008000;color: #CCC;">&#40;</span>rect<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
QGraphicsEllipseItem <span style="color: #ffff00;">*</span>circle<span style="color: #008080;">;</span>
circle <span style="color: #ffffff;">=</span> <span style="color: #0000dd;color: #8FB394;">new</span> QGraphicsEllipseItem<span style="color: #008000;color: #CCC;">&#40;</span>QRect<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">100</span>, <span style="color: #0000dd;color: #DDD;">100</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
circle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setBrush<span style="color: #008000;color: #CCC;">&#40;</span> Qt<span style="color: #008080;">::</span><span style="color: #007788;">blue</span> <span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
circle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setPos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">120</span>, <span style="color: #0000dd;color: #DDD;">10</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
scene.<span style="color: #007788;">addItem</span><span style="color: #008000;color: #CCC;">&#40;</span>circle<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
QGraphicsView view<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #00ff00;">&amp;</span>scene<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
view.<span style="color: #007788;">show</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<div id="attachment_181" class="wp-caption aligncenter" style="width: 195px"><img class="size-full wp-image-181" title="rect_circle" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/ex1.png" alt="A rectangle and a circle. No transformations applied." width="185" height="103" /><p class="wp-caption-text">A rectangle and a circle. No transformations applied.</p></div>
<p>To add both items, the <a href="http://doc.trolltech.com/4.5/qgraphicsscene.html#addItem">addItem</a> method was used. Both bounding rectangles start at (0, 0) point, but their scene positions were changed through the <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html#setPos">setPos</a> method. You can still use the convenience methods to add items if you want (addRect, addEllipse, addPolygon, etc).</p>
<p>In order to add a third item, we must enlarge the scene to visualize it.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;">QGraphicsScene scene<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">240</span>, <span style="color: #0000dd;color: #DDD;">200</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;">QPolygonF polygon<span style="color: #008080;">;</span>
polygon <span style="color: #ffffff;">&lt;&lt;</span> QPointF<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">60</span><span style="color: #008000;color: #CCC;">&#41;</span> <span style="color: #ffffff;">&lt;&lt;</span> QPointF<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">120</span>, <span style="color: #0000dd;color: #DDD;">60</span><span style="color: #008000;color: #CCC;">&#41;</span>
                          <span style="color: #ffffff;">&lt;&lt;</span> QPointF<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">60</span>, <span style="color: #0000dd;color: #DDD;">0</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
QGraphicsPolygonItem <span style="color: #ffff00;">*</span>triangle<span style="color: #008080;">;</span>
triangle <span style="color: #ffffff;">=</span> <span style="color: #0000dd;color: #8FB394;">new</span> QGraphicsPolygonItem<span style="color: #008000;color: #CCC;">&#40;</span>polygon<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
triangle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setBrush<span style="color: #008000;color: #CCC;">&#40;</span> Qt<span style="color: #008080;">::</span><span style="color: #007788;">green</span> <span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
triangle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>setPos<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">60</span>, <span style="color: #0000dd;color: #DDD;">120</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
scene.<span style="color: #007788;">addItem</span><span style="color: #008000;color: #CCC;">&#40;</span>triangle<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
...</pre></div></div>

<div id="attachment_186" class="wp-caption aligncenter" style="width: 204px"><img class="size-full wp-image-186" title="rect_circle_triangle" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/ex2.png" alt="A triangle (QGraphicsPolygonItem) added to the scene." width="194" height="164" /><p class="wp-caption-text">A triangle (QGraphicsPolygonItem) added to the scene.</p></div>
<p>The triangle was created with the <a href="http://doc.trolltech.com/4.5/qgraphicspolygonitem.html">QGraphicsPolygonItem</a> class.  A QPolygonF with the vertices is passed to its constructor.</p>
<p>Now, let&#8217;s apply some operations to them:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;">rect<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>translate<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">15</span>, <span style="color: #0000dd;color: #DDD;">10</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
rect<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>rotate<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">10</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
circle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>translate<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">20</span>, <span style="color: #0000dd;color: #DDD;">30</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
circle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>scale<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">1</span><span style="color: #ffff00;">/</span><span style="color:#800080;">1.5</span>, <span style="color: #0000dd;color: #DDD;">1</span><span style="color: #ffff00;">/</span><span style="color:#800080;">1.5</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
triangle<span style="color: #ffff00;">-</span><span style="color: #ffffff;">&gt;</span>shear<span style="color: #008000;color: #CCC;">&#40;</span><span style="color:#800080;">0.85</span>, <span style="color: #0000dd;color: #DDD;">0</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
...</pre></div></div>

<div id="attachment_191" class="wp-caption aligncenter" style="width: 205px"><img class="size-full wp-image-191" title="rect_circle_tri_transformed" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/ex3.png" alt="Items rotated, translated, scaled and sheared." width="195" height="163" /><p class="wp-caption-text">Items rotated, translated, scaled and sheared.</p></div>
<p>Now the rectangle was moved and rotated (by default Qt sets the transformation center to the scene&#8217;s (0,0) point), the circle was moved and scaled (shrinked) and the triangle was sheared. You can learn more about transformations on this link: <a href="http://doc.trolltech.com/4.5/qgraphicsitem.html">QGraphicsItem class</a>.</p>
<p>And that&#8217;s it. Hope this was usefull. The source code used for this post is available here: <a href="http://fredyduarte.net/blog/uploads/qgraphicsitem-examples.tar.gz">qgraphicsitem-examples.tar.gz</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=110</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>QGraphicsView framework</title>
		<link>http://fredyduarte.net/blog/?p=7</link>
		<comments>http://fredyduarte.net/blog/?p=7#comments</comments>
		<pubDate>Sat, 02 May 2009 15:10:52 +0000</pubDate>
		<dc:creator>fred</dc:creator>
				<category><![CDATA[Qt]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=7</guid>
		<description><![CDATA[This is just a small introduction to Qt Canvas system. I&#8217;ll give a brief overview of the QGraphicsView framework.
In Qt, it is possible to create your own widgets through the standard provided classes like QPushButton, QCheckBox, QGroupBox, etc. But even these widgets have limitations when writing a fully customizable application. Taking into account that most [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">This is just a small introduction to Qt Canvas system. I&#8217;ll give a brief overview of the <strong>QGraphicsView</strong> framework.</p>
<p style="text-align: left;">In Qt, it is possible to create your own widgets through the standard provided classes like <em>QPushButton</em>, <em>QCheckBox</em>, <em>QGroupBox</em>, etc. But even these widgets have limitations when writing a fully customizable application. Taking into account that most applications are built around Two-dimensional canvas, <a title="Qt Software" href="http://www.qtsoftware.com/" target="_self">Qt Software</a> developed the Graphics View framework, namely <strong>QGraphicsView</strong>.</p>
<p style="text-align: left;"><strong>QGraphicsView</strong> was preceded by the former two-dimensional canvas, <em>QCanvas</em>. The framework is composed by 3 main elements:</p>
<ol>
<li><strong><span style="text-decoration: underline;">QGraphicsView</span></strong>: a class for displaying the widgets of a scene; It represents a viewport.</li>
<li><strong><span style="text-decoration: underline;">QGraphicsScene</span></strong>: a class for storing the widgets, handling event propagation(input from mouse, keyboard and other devices) and managing item states. It represents a scene with items in it;</li>
<li><strong><span style="text-decoration: underline;">QGraphicsItem</span></strong>: a basic class for the graphical items on the scene. It can also represent a group of items.</li>
</ol>
<p>A <strong>QGraphicsScene</strong> object is flexible enough to include any number of <strong>QGraphicsItem</strong> objects and still mantain the efficiency in retrieving them. On the other hand, a <strong>QGraphicsView</strong> object size is limited by the computer&#8217;s display size(1280&#215;1024, 1024&#215;768, 800&#215;600, etc). Putting these 3 elements together we have something like this:</p>
<div id="attachment_105" class="wp-caption aligncenter" style="width: 410px"><img class="size-full wp-image-105" title="QGraphicsView framework" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/qgraphicsview_400x437.png" alt="qgraphicsview_400x437" width="400" height="437" /><p class="wp-caption-text">A sample composition of the QGraphicsView. Notice that the entire scene(QGraphicsScene) is not seen on the viewport.</p></div>
<p style="text-align: left;">The idea is to manage the events of each item you create and redraw them as needed. This way, you can make your own widgets with more visually appealing features.</p>
<p>Now, let&#8217;s take a look at a small example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;">QGraphicsScene scene<span style="color: #008080;">;</span>
&nbsp;
scene.<span style="color: #007788;">addText</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #FF0000;color: #666666;">&quot;Hello, world!&quot;</span>, QFont<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #FF0000;color: #666666;">&quot;Times&quot;</span>, <span style="color: #0000dd;color: #DDD;">10</span>, QFont<span style="color: #008080;">::</span><span style="color: #007788;">Bold</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
QGraphicsView view<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #00ff00;">&amp;</span>scene<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
view.<span style="color: #007788;">show</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>In the code above, I&#8217;ve created a <strong>QGraphicsScene</strong> object and added a text to it through the convenience function <a href="http://doc.trolltech.com/4.5/qgraphicsscene.html#addText" target="_self">QGraphicsScene::addText</a>. The type of the item added is a <a href="http://doc.trolltech.com/4.5/qgraphicstextitem.html" target="_self">QGraphicsTextItem</a>. Then a <strong>QGraphicsView</strong> object is created to display the scene. And the result is:</p>
<div id="attachment_106" class="wp-caption aligncenter" style="width: 212px"><img class="size-full wp-image-106" title="Hello World" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/hello-world.png" alt="Hello World example." width="202" height="102" /><p class="wp-caption-text">Hello World example.</p></div>
<p>Let&#8217;s modify it a little bit:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;color: #FCFFBA;">QGraphicsScene scene<span style="color: #008080;">;</span>
&nbsp;
QPainterPath path<span style="color: #008080;">;</span>
path.<span style="color: #007788;">moveTo</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">10</span>, <span style="color: #0000dd;color: #DDD;">30</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
path.<span style="color: #007788;">cubicTo</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #0000dd;color: #DDD;">80</span>, <span style="color: #0000dd;color: #DDD;">0</span>, <span style="color: #0000dd;color: #DDD;">50</span>, <span style="color: #0000dd;color: #DDD;">50</span>, <span style="color: #0000dd;color: #DDD;">80</span>, <span style="color: #0000dd;color: #DDD;">80</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
scene.<span style="color: #007788;">addPath</span><span style="color: #008000;color: #CCC;">&#40;</span>path, QPen<span style="color: #008000;color: #CCC;">&#40;</span>Qt<span style="color: #008080;">::</span><span style="color: #007788;">black</span><span style="color: #008000;color: #CCC;">&#41;</span>, QBrush<span style="color: #008000;color: #CCC;">&#40;</span>Qt<span style="color: #008080;">::</span><span style="color: #007788;">green</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
scene.<span style="color: #007788;">addText</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #FF0000;color: #666666;">&quot;Hello, world!&quot;</span>, QFont<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #FF0000;color: #666666;">&quot;Times&quot;</span>, <span style="color: #0000dd;color: #DDD;">10</span>, QFont<span style="color: #008080;">::</span><span style="color: #007788;">Bold</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
QGraphicsView view<span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #00ff00;">&amp;</span>scene<span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span>
view.<span style="color: #007788;">show</span><span style="color: #008000;color: #CCC;">&#40;</span><span style="color: #008000;color: #CCC;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Now I&#8217;ve also added a path to scene. This particular path creates a Bezier curve in green color. To create the path, it&#8217;s used the <a href="http://doc.trolltech.com/4.5/qpainterpath.html" target="_self">QPainterPath</a> class, which allows you to draw any shape you want. And like the addText function, there&#8217;s also a <a href="http://doc.trolltech.com/4.5/qgraphicsscene.html#addPath" target="_self">QGraphicsScene::addPath</a> convenience function. The result is:</p>
<div id="attachment_107" class="wp-caption aligncenter" style="width: 212px"><img class="size-full wp-image-107" title="Hello World Bezier" src="http://fredyduarte.net/blog/wp-content/uploads/2009/05/hello-world-bezier.png" alt="hello-world-bezier" width="202" height="102" /><p class="wp-caption-text">Hello World example with a Path item.</p></div>
<p style="text-align: center;">
<p>Every <strong>QGraphicsItem</strong> on the scene can also be translated, rotated, scaled and sheared. The <strong>QGraphicsView</strong> class also supports these four operations.</p>
<p>And that&#8217;s it. Next time I&#8217;ll show some more advanced examples of the framework usage.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>End of Activities</title>
		<link>http://fredyduarte.net/blog/?p=1</link>
		<comments>http://fredyduarte.net/blog/?p=1#comments</comments>
		<pubDate>Mon, 20 Apr 2009 23:18:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://fredyduarte.net/blog/?p=1</guid>
		<description><![CDATA[This is the final post of this blog.
]]></description>
			<content:encoded><![CDATA[<p>This is the final post of this blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredyduarte.net/blog/?feed=rss2&amp;p=1</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
