<?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>Coding Friends &#187; signals</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/signals/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>QT &#8211; hello world &#8211; signals and slots</title>
		<link>http://www.codingfriends.com/index.php/2010/02/24/qt-hello-world-signals-and-slots/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=qt-hello-world-signals-and-slots</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/24/qt-hello-world-signals-and-slots/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 12:20:49 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[QT]]></category>
		<category><![CDATA[signals]]></category>
		<category><![CDATA[slots]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=756</guid>
		<description><![CDATA[QT framework is a great application stack that allows for cross development (between different OS&#8217;s) without having to re-do the code for e.g.Linux/Windows different setups. It is similar to Java and C Sharp (C#) in that way, but the difference with them is that they compile into a native object which can then run within [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Qt_%28framework%29">QT framework</a> is a great application stack that allows for cross development (between different OS&#8217;s) without having to re-do the code for e.g.Linux/Windows different setups.  It is similar to Java and C Sharp (C#) in that way, but the difference with them is that they compile into a native object which can then run within a virtual machine where as the QT framework is more designed to be compiled within each setup, which should make it quicker as well because it will be in the native code and not running in a virtual machine.</p>
<p>The nice thing about QT is that it has its own SIGNAL and SLOTS, similar to C Sharp (C#) <a href="http://www.codingfriends.com/index.php/2010/02/19/events-fire-that-event/">events</a> process where you can link something happening to when something else has just happened (e.g. moved a value on a slider bar and a integer value alters as well).</p>
<p>I shall go into more detail with SIGNAL&#8217;s and SLOT&#8217;s in the next tutorial for QT development, but in essence they are defined within the class of the object itself and then you link them with a &#8220;connect&#8221; syntax. e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    object<span style="color: #008080;">::</span><span style="color: #007788;">connect</span><span style="color: #008000;">&#40;</span>object_to_send_signal, SIGNAL<span style="color: #008000;">&#40;</span>signal_of_the_object_that_will_be_emit<span style="color: #008000;">&#41;</span>,
                        object_to_pick_up_signal, SLOT<span style="color: #008000;">&#40;</span>function_to_call_once_signal_has_been_emitted<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>so you will link/connect a objects that sends a signal to a slot that receives the signal.</p>
<p>Here is a basic GUI for a QT application that will have a button that says &#8220;QUIT&#8221; and also a signal that is emitted once that button class clicked() has happened, that links to the application (QApplication object) that has a &#8220;quit&#8221; function that stops the program from executing. </p>
<p>To start with we first include the basic header files and then create a QApplication (the gui as such) and then a QPushButton object with the default text being &#8220;Quit&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    QApplication app<span style="color: #008000;">&#40;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    QPushButton <span style="color: #000040;">*</span>button <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QPushButton<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Quit&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>once this has happened we then create the connection between the clicking of the button to the application &#8220;quit&#8221;ing function.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    QObject<span style="color: #008080;">::</span><span style="color: #007788;">connect</span><span style="color: #008000;">&#40;</span>button, SIGNAL<span style="color: #008000;">&#40;</span>clicked<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
                        <span style="color: #000040;">&amp;</span>app, SLOT<span style="color: #008000;">&#40;</span>quit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>we have only created the button but not placed it on the screen, so we use the show method within the button class and then run the application execution function to actually run the GUI program</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    button<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>show<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> app.<span style="color: #007788;">exec</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>if you save this file within a directory called quit and call this file quit.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;QApplication&gt;</span>
<span style="color: #339900;">#include &lt;QPushButton&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    QApplication app<span style="color: #008000;">&#40;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    QPushButton <span style="color: #000040;">*</span>button <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QPushButton<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Quit&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    QObject<span style="color: #008080;">::</span><span style="color: #007788;">connect</span><span style="color: #008000;">&#40;</span>button, SIGNAL<span style="color: #008000;">&#40;</span>clicked<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
                        <span style="color: #000040;">&amp;</span>app, SLOT<span style="color: #008000;">&#40;</span>quit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    button<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>show<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> app.<span style="color: #007788;">exec</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>then change to that directory within your console/command line and then we need to build up the QT compiling project files.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">qmake <span style="color: #660033;">-project</span></pre></div></div>

<p>this will make the quit.pro file within that same directory (it uses the same name as the directory name), and within that file is</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">######################################################################</span>
<span style="color: #666666; font-style: italic;"># Automatically generated by qmake (2.01a) Wed Feb 24 11:49:39 2010</span>
<span style="color: #666666; font-style: italic;">######################################################################</span>
&nbsp;
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
&nbsp;
<span style="color: #666666; font-style: italic;"># Input</span>
SOURCES += quit.cpp</pre></div></div>

<p>which basically means that the application uses the source files of quit.cpp, you then need to create the Makefile (which is the actual compiling c++ file that g++ uses to create the application)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">qmake</pre></div></div>

<p>so the directory will be something similar to</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Makefile  
quit.cpp
quit.pro</pre></div></div>

<p>within Linux you can just now run the make command which will make the application &#8220;quit&#8221; but without windows you would need to have the <a href="http://msdn.microsoft.com/en-us/library/dd9y37ha%28VS.71%29.aspx">nmake</a> command instead, so run either make or nmake and within the directory now will be a quit application that you can run and the output would be a simple GUI with a button on it that says &#8220;Quit&#8221; and once you click that button the application will quit.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/24/qt-hello-world-signals-and-slots/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

