<?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>Dave Smith&#039;s Blog</title>
	<atom:link href="http://thesmithfam.org/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://thesmithfam.org/blog</link>
	<description>Your blog is probably better than mine.</description>
	<lastBuildDate>Mon, 08 Feb 2010 05:59:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Talking to Qt Threads</title>
		<link>http://thesmithfam.org/blog/2010/02/07/talking-to-qt-threads/</link>
		<comments>http://thesmithfam.org/blog/2010/02/07/talking-to-qt-threads/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 05:59:31 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=609</guid>
		<description><![CDATA[I wrote about multi-threading in Qt a while back, and how to use QThread to wrap a blocking function call &#8220;lock free&#8221;. Today we&#8217;ll talk about how to pass data into your thread. This approach can be used, for example, to tell your QThread to stop.

pre { font-size: 10pt; border: 2px solid #016; padding: 5px; [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote about <a href="http://thesmithfam.org/blog/2009/09/30/lock-free-multi-threading-in-qt/">multi-threading in Qt</a> a while back, and how to use <b>QThread</b> to wrap a blocking function call &#8220;lock free&#8221;. Today we&#8217;ll talk about how to pass data into your thread. This approach can be used, for example, to tell your <b>QThread</b> to stop.</p>
<style>
pre { font-size: 10pt; border: 2px solid #016; padding: 5px; color: white; background: #333; }
</style>
<p>There are two ways to use <b>QThread</b>, with and without an event loop, and the preferred method for talking to a <b>QThread</b> depends on which of them you use.</p>
<p><b>Way 1: Without an event loop</b></p>
<p>When you&#8217;re not using an event loop, the thread&#8217;s <b>run()</b> method often looks like this:</p>
<pre>
void MyThread::run()
{
    forever
    {
        // Do some stuff
    }
}
</pre>
<p>Since this method does not use an event loop, there is no way to deliver a signal to your thread. So if you want to pass data into the thread, you have to use a good old fashioned mutex. Let&#8217;s look at an example showing you how to <b>stop</b> your thread:</p>
<pre>
void MyThread::run()
{
    forever
    {
        {
            // "mutex" and "stopRequested" are member
            // variables of MyThread:
            QMutexLocker locker(&#038;mutex);
            if(stopRequested)
                return;
        }

        // Do some stuff
    }
}

void MyThread::stop()
{
    QMutexLocker locker(&#038;mutex);
    stopRequested = true;
}
</pre>
<p>In this case, we have to check the <b>stopRequested</b> variable in a timely manner in our thread&#8217;s <b>run()</b> method. The longer you run between checks, the longer it will take your thread to actually stop.</p>
<p>Outside observers can use the <b>finished()</b> signal to know when your thread is actually done. So if you are in a QMainWindow, for example, and a <b>closeEvent()</b> happens, you can ignore the event, call <b>MyThread::stop()</b>, and then when the <b>QThread::finished()</b> signal arrives, you can actually close the window.</p>
<p>The downside is that the <b>stop()</b> call will actually block while it tries to acquire the <b>mutex</b>. Given the way this code is written, the blocking will probably be very short, but hey, I hate blocking. Let&#8217;s see if we can dig up a better way to do this.</p>
<p><b>Way 2: With an event loop</b></p>
<p>If you have an event loop, you can use Qt&#8217;s meta-objects to talk to your thread. Let&#8217;s look at the same example as before, only this time with no locking or blocking.</p>
<pre>
void MyThread::MyThread()
{
    moveToThread(this);
}

void MyThread::run()
{
    QTimer *timer = new QTimer();
    connect(timer, SIGNAL(timeout()),
        this, SLOT(doSomething()));
    timer->start(0);

    exec(); // starts the event loop, and doesn't
            // return until it is told to quit()
}

void MyThread::stop()
{
    if(currentThread() != this)
    {
        // The caller is running in a
        // different thread, so use Qt to
        // call stop() later, on our own thread:
        QMetaObject::invokeMethod(this, "stop",
                        Qt::QueuedConnection);
    }
    else
    {
        // Now the call has arrived from our
        // own thread, yay! We can safely
        // shut down our event loop.
        quit();
    }
}
</pre>
<p>Look mom! No locks! Now we have killed our thread, safely and gracefully. There is no chance of blocking, and we learned something about QMetaObject.</p>
<p>A couple items to note:</p>
<ul>
<li>The <b>doSomething()</b> method is left as an exercise for the reader, but be careful about the <b>QTimer</b> interval. I used <b>0</b>, which means it will be firing almost constantly.</li>
<li>The <b>stop()</b> method must be a <b>slot</b> in MyThread (and not just a regular method) or else <b>invokeMethod()</b> will return false and not actually re-invoke <b>stop()</b> for you.</li>
<li>You <b>can</b> pass arguments to your thread this way, but it requires a bit more fun with <a href="http://doc.trolltech.com/4.6/qmetaobject.html">QMetaObject::invokeMethod()</a>.</li>
<li>You can reduce this whole thing to a magical macro that you could put at the top of your stop() method, saving you from having to write <b>if(currentThread() == this)</b> at the top of every method. Hint: use the __FUNCTION__ macro.</li>
<li>To run this example code, you&#8217;ll need to <b>#include</b> these files: QThread, QTimer, QMetaObject, QMutexLocker, and QMutex</li>
<li>To call <b>quit()</b>, it may not actually be necessary to be running on the same <b>QThread</b> (it works for me without the QMetaObject), but this will be required when you start passing in data to your thread. Without it, your program could do unpredictable naughty things. I can&#8217;t find anything in the docs about whether <b>quit()</b> is thread safe.
</li>
</ul>
<p>I&#8217;ve found this <b>QMetaObject</b> approach the most effective and easiest way to pass data to <b>QThreads</b> safely, without blocking and without locks.</p>
<p>Happy threading!</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2010/02/07/talking-to-qt-threads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Person Video on a Shoestring</title>
		<link>http://thesmithfam.org/blog/2010/02/03/first-person-video-on-a-shoestring/</link>
		<comments>http://thesmithfam.org/blog/2010/02/03/first-person-video-on-a-shoestring/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 03:14:53 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[RC Planes]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=597</guid>
		<description><![CDATA[Today my friend Cliff showed me his first-person video R/C airplane:

This thing has every feature imaginable, which means it packs a lot of wires:

Here&#8217;s a video tour of all its features, which include the following:

Auto fly-home if control is lost (uses GPS)
Heads up video display of altitude, heading, and pitch
RF beacon if the plane is [...]]]></description>
			<content:encoded><![CDATA[<p>Today my friend Cliff showed me his first-person video R/C airplane:</p>
<p><img src="/images/cliffs-fpv-wild-hawk.jpg" /></p>
<p>This thing has every feature imaginable, which means it packs a lot of wires:</p>
<p><img src="/images/cliffs-fpv-wild-hawk-closeup.jpg" /></p>
<p>Here&#8217;s a video tour of all its features, which include the following:</p>
<ul>
<li>Auto fly-home if control is lost (uses GPS)</li>
<li>Heads up video display of altitude, heading, and pitch</li>
<li>RF beacon if the plane is lost</li>
<li>Auto stabilization using FMA co-pilot</li>
<li>Data logger providing an audible report of battery life and other stats</li>
<li>VR goggles for watching the action</li>
<li>Pan and tilt camera, controllable from the radio</li>
<li>High-power transmitter (he&#8217;s flown a mile away on FPV)</li>
</ul>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/ZuYbWzJIUCc&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ZuYbWzJIUCc&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2010/02/03/first-person-video-on-a-shoestring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to (really) change the calendar colors on your iPhone</title>
		<link>http://thesmithfam.org/blog/2010/01/23/how-to-really-change-the-calendar-colors-on-your-iphone/</link>
		<comments>http://thesmithfam.org/blog/2010/01/23/how-to-really-change-the-calendar-colors-on-your-iphone/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 05:10:35 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=578</guid>
		<description><![CDATA[The iPhone doesn&#8217;t (easily) let you change the colors of your calendar items. Couple this with the fact that there are lots of ways to setup calendar sync&#8217;ing, and it&#8217;s easy to get discouraged.
For me, my work calendar and my personal calendar both showed up as nearly the same shade of red. Fail.
Here&#8217;s how I [...]]]></description>
			<content:encoded><![CDATA[<p>The iPhone doesn&#8217;t (easily) let you change the colors of your calendar items. Couple this with the fact that there are lots of ways to setup calendar sync&#8217;ing, and it&#8217;s easy to get discouraged.</p>
<p>For me, my work calendar and my personal calendar both showed up as nearly the same shade of red. Fail.</p>
<p>Here&#8217;s how I fixed it. This only works if you have the following situation:</p>
<ul>
<li>iPhone OS 3.0 or newer</li>
<li>Using Google Calendar with CalDAV (<b>not</b> Exchange)</li>
<li>Note: I use Exchange for my work calendar and Google for my personal calendar, and I want them <b>both</b> on my iPhone with different colors</li>
</ul>
<p>Steps to change the color (this takes about 5 minutes if nothing goes wrong).</p>
<ol>
<li>Open iCal on your Mac (don&#8217;t have a Mac? Maybe Mozilla Sunbird can do this too)</li>
<li>Subscribe to your Google Calendar (<a href="http://www.google.com/support/calendar/bin/answer.py?hl=en&#038;answer=99358#ical">instructions to do this</a></li>
<li>In the sidebar, notice your new calendar appears<br />
<img src="/images/ical-with-google.png" /></li>
<li>Right-click on the new calendar and select <b>Get Info</b></li>
<li>Now change the color to what you want<br />
<img style="margin-left: -55px" src="/images/ical-with-google-color.png" /></li>
<li>Do nothing, and in a couple minutes your iPhone calendar items will have the new color</li>
</ol>
<p>If you can&#8217;t get iCal setup, don&#8217;t fret. It does work. Double check your URL you typed to subscribe to your Google Calendar. Don&#8217;t forget the &#8220;/user&#8221; at the end. Follow Google&#8217;s instructions (see link above) very closely.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2010/01/23/how-to-really-change-the-calendar-colors-on-your-iphone/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Functional is not Procedural</title>
		<link>http://thesmithfam.org/blog/2010/01/05/functional-is-not-procedural/</link>
		<comments>http://thesmithfam.org/blog/2010/01/05/functional-is-not-procedural/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 22:17:46 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=562</guid>
		<description><![CDATA[I&#8217;ve recently seen some confusion about the terms functional and procedural in the software development community. Let me spend a moment trying to clear things up for (both of) my readers.
First of all, just because a programming language has functions, that does not mean it is functional. In fact, its confusing name has nothing to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently seen some confusion about the terms <b>functional</b> and <b>procedural</b> in the software development community. Let me spend a moment trying to clear things up for (both of) my readers.</p>
<p>First of all, just because a programming language has <b>functions</b>, that does not mean it is <b>functional</b>. In fact, its confusing name has nothing to do with functions. The history of the term <b>function</b>, as used in the computation world, hails back to the 1930&#8217;s when Alonzo Church created Lambda Calculus to be able to use and define mathematical algorithms on paper. Later, when computers arrived on the scene, programmers adopted the term <b>function</b> to describe a chunk of code that could be re-used by other chunks of code. Functions, in this context, have been adopted by every programming language I know of, including assembly language. Functions are commonly referred to as &#8220;routines&#8221;, &#8220;subroutines&#8221;, or &#8220;procedures&#8221;. Some might even argue that a &#8220;method&#8221; is pragmatically identical to a function.</p>
<p>Here&#8217;s where the confusion begins.</p>
<p>This is hard to explain rigorously, but here goes anyway.</p>
<p>Some languages, like C, tend to encourage computer programmers to make use of functions. Programs written in these languages tend to consist of a set of functions, and a &#8220;main&#8221; function that kicks everything off. Such a language is often referred to as &#8220;procedural&#8221;, but really the term &#8220;procedural&#8221; refers to a style of programming, and does not really describe a language, per se. It is most often used as an antonym to the concept of &#8220;object oriented&#8221;. But, oddly, it has nothing to do with &#8220;functional&#8221; programming.</p>
<p>Other languages, like Scheme and Haskell, tend to encourage computer programmers to write code with functions that avoid internal state changes and produce no side effects when called. For example, these language discourage global variables, and particularly the calling of functions that would mutate global variables (that would be a side effect).</p>
<p>To sum up.</p>
<p>Procedural programming languages can be functional or not. Functional programming languages can be procedural or not.</p>
<p>Here&#8217;s a table:</p>
<style><!--
table { border: 1px solid black; border-collapse: collapse; }
table td, table th { padding: 3px; border: 1px solid black; }
--></style>
<table>
<tr>
<th>Language</th>
<th>Functional</th>
<th>Procedural</th>
</tr>
<tr>
<td>C</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Haskell</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>Python</td>
<td>Can be</td>
<td>Can be</td>
</tr>
<tr>
<td>C++</td>
<td>No</td>
<td>Can be (if you write it like C)</td>
</tr>
<tr>
<td>Java</td>
<td>No</td>
<td>Usually not</td>
</tr>
<tr>
<td>OCaml</td>
<td>Yes</td>
<td>Yes</td>
</tr>
</table>
<p>If you take nothing else away from this article, take this: The C programming language is <b>not</b> a functional language. It&#8217;s not bad, it&#8217;s just not functional.</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2010/01/05/functional-is-not-procedural/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Technology Predictions for 2010</title>
		<link>http://thesmithfam.org/blog/2010/01/01/technology-predictions-for-2010/</link>
		<comments>http://thesmithfam.org/blog/2010/01/01/technology-predictions-for-2010/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 16:30:54 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>
		<category><![CDATA[Miscellany]]></category>
		<category><![CDATA[RC Planes]]></category>
		<category><![CDATA[2010]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=539</guid>
		<description><![CDATA[My brain&#8217;s been a stewing about 2010, and I just gotta let it all out. If I&#8217;m even half right, 2010 will be awesome.
1. Hulu in the Living Room

In 2010, Hulu will partner with a company like Western Digital or Popcorn Hour to release a tiny box that connects to your HDTV to watch TV [...]]]></description>
			<content:encoded><![CDATA[<p>My brain&#8217;s been a stewing about 2010, and I just gotta let it all out. If I&#8217;m even half right, 2010 will be awesome.</p>
<h2>1. Hulu in the Living Room</h2>
<p><img src="/images/hulu.png" style="float: right; margin-left: 20px; margin-bottom: 0px;" /></p>
<p>In 2010, <a href="http://hulu.com">Hulu</a> will partner with a company like <a href="http://www.wdc.com/en/products/WDTV/">Western Digital</a> or <a href="http://www.popcornhour.com/">Popcorn Hour</a> to release a tiny box that connects to your HDTV to watch TV shows and movies, streamed from Hulu&#8217;s servers. The box will cost less than $100, and have no monthly subscription, because its development will be subsidized by Hulu&#8217;s ad revenues. If it&#8217;s not released in 2010, it will at least be announced.</p>
<h2>2. Cable Networks Become Record Labels</h2>
<p><img src="/images/itunes.png" style="float: right; margin-left: 20px; margin-bottom: 0px;" /></p>
<p>Just like iTunes changed the music industry, online media and broadband Internet access will start to change the TV industry. It remains to be seen who will be the big player (my money&#8217;s on <a href="http://hulu.com">Hulu</a>), but one thing is for sure: Americans will have a choice when it comes to buying subscription TV services. The trend will begin in 2010, allowing consumers to get on-demand TV content for free, and will finish some time in the future with cable companies either totally transforming into something like modern record labels, or going out of business.</p>
<h2>3. HDTV Sales Will Boom</h2>
<p><img src="/images/hdtv.png" style="float: right; margin-left: 20px; margin-bottom: 0px;" /></p>
<p>HDTV&#8217;s have gotten so inexpensive that nearly every American household will have one by the end of 2010. This will be driven by the fact that online media will be so easily accessible that most people won&#8217;t need to buy cable, so they can easily justify the cost of a new $500 TV.</p>
<h2>4. Qt&#8217;s Best Year</h2>
<p><img src="/images/qt-logo.png" style="float: right; margin-left: 20px; margin-bottom: 0px;" /></p>
<p>Companies who develop desktop application software that needs to run on Mac and Windows will simply have no other choice for their developers than Qt. That trend will strengthen in 2010 to the point that it&#8217;s a no-brainer decision over platform-specific choices like .NET, Cocoa, and others. Qt has matured so much over the last 5 years that its toe-hold in this market will grow to a full Nelson in 2010.</p>
<h2>5. Model Aviation Takes Off</h2>
<p><img src="/images/super-cub.png" style="float: right; margin-left: 20px; margin-bottom: 0px;" /></p>
<p>Model airplanes have gotten very popular and accessible. Manufacturers have pushed prices down so low that anyone can afford to get into the hobby. Batteries, R/C electronics, and planes have gotten cheaper, and planes have gotten easier to fly for the novice pilot. The entry-level model airplane market will take off in 2010.</p>
<h2 style="color: #061">Your Turn</h2>
<p>Do you agree with my predictions, or am I totally off?</p>
<p>Have any predictions of your own?</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2010/01/01/technology-predictions-for-2010/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Apple Time Machine User Interface Win</title>
		<link>http://thesmithfam.org/blog/2009/10/25/apple-time-machine-user-interface-win/</link>
		<comments>http://thesmithfam.org/blog/2009/10/25/apple-time-machine-user-interface-win/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 04:26:53 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=523</guid>
		<description><![CDATA[Earlier this year, a shiny new iMac graced my home (increasing my resale value by about 10%, according to Zillow). Since then, I&#8217;ve had to replace my keyboard three times due to drool damage.
It happened again tonight, this time compliments of the Time Machine menu bar icon (I know, it&#8217;s a curse).

I noticed months ago [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this year, a shiny new iMac graced my home (increasing my resale value by about 10%, according to <a href="http://www.zillow.com/">Zillow</a>). Since then, I&#8217;ve had to replace my keyboard three times due to drool damage.</p>
<p>It happened again tonight, this time compliments of the <a href="http://www.apple.com/macosx/what-is-macosx/time-machine.html">Time Machine</a> menu bar icon (I know, it&#8217;s a curse).</p>
<p><img src="/images/time-machine-menubar-icon.png" /></p>
<p>I noticed months ago that the icon spins while Time Machine is backing up my iMac. But that&#8217;s not the drool feature. The little hands on the clock spin, but they go <b>backwards</b>! It&#8217;s a gentle reminder that Time Machine lets you go <b>back in time</b> to restore your files.</p>
<p>Awesome, but now it&#8217;s time for another new <a href="http://www.apple.com/keyboard/">keyboard</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2009/10/25/apple-time-machine-user-interface-win/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>R/C Stryker Slope Glider</title>
		<link>http://thesmithfam.org/blog/2009/10/14/rc-stryker-slope-glider/</link>
		<comments>http://thesmithfam.org/blog/2009/10/14/rc-stryker-slope-glider/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 19:50:35 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[RC Planes]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=520</guid>
		<description><![CDATA[I finally took my newly built Stryker to the slope for some unpowered slope gliding at Ensign peak in Salt Lake City, Utah. I built the Stryker with these parts:

Stryker Fuselage from ParkZone
A tiny 2-cell LiPo battery
Two cheap servos
A cheap voltage converter

I did not put a motor on the Stryker and used a tiny battery. [...]]]></description>
			<content:encoded><![CDATA[<p>I finally took my newly built Stryker to the slope for some unpowered slope gliding at Ensign peak in Salt Lake City, Utah. I built the Stryker with these parts:</p>
<ul>
<li><a href="http://parkzone.com/Products/Default.aspx?ProdID=PKZ1267">Stryker Fuselage from ParkZone</a></li>
<li>A tiny 2-cell LiPo battery</li>
<li><a href="http://hobbycity.com/hobbycity/store/uh_viewItem.asp?idProduct=663">Two cheap servos</a></li>
<li><a href="http://hobbycity.com/hobbycity/store/uh_viewItem.asp?idProduct=3735">A cheap voltage converter</a></li>
</ul>
<p>I did not put a motor on the Stryker and used a tiny battery. It slopes beautifully in 15-20mph wind. In fact, I had to add some weight to get better penetration.</p>
<p>I highly recommend the Stryker for slope flying.</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2009/10/14/rc-stryker-slope-glider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool QProgressBar Stylesheet</title>
		<link>http://thesmithfam.org/blog/2009/10/13/cool-qprogressbar-stylesheet/</link>
		<comments>http://thesmithfam.org/blog/2009/10/13/cool-qprogressbar-stylesheet/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 20:28:51 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=509</guid>
		<description><![CDATA[Qt&#8217;s powerful stylesheet system can make your boring progress bars look really cool.
Screenshot:

Here&#8217;s the code:
QProgressBar {
border: 1px solid black;
text-align: top;
padding: 1px;
border-bottom-right-radius: 7px;
border-bottom-left-radius: 7px;
background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,
stop: 0 #fff,
stop: 0.4999 #eee,
stop: 0.5 #ddd,
stop: 1 #eee );
width: 15px;
}
QProgressBar::chunk {
background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,
stop: 0 #78d,
stop: 0.4999 [...]]]></description>
			<content:encoded><![CDATA[<p>Qt&#8217;s powerful stylesheet system can make your boring progress bars look really cool.</p>
<p><b>Screenshot:</b></p>
<p><img src="/images/qt-progress-bar-style-sheet.png" /></p>
<p><b>Here&#8217;s the code:</b></p>
<p><code style="whitespace: nowrap">QProgressBar {<br />
border: 1px solid black;<br />
text-align: top;<br />
padding: 1px;<br />
border-bottom-right-radius: 7px;<br />
border-bottom-left-radius: 7px;<br />
background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,<br />
stop: 0 #fff,<br />
stop: 0.4999 #eee,<br />
stop: 0.5 #ddd,<br />
stop: 1 #eee );<br />
width: 15px;<br />
}</p>
<p>QProgressBar::chunk {<br />
background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,<br />
stop: 0 #78d,<br />
stop: 0.4999 #46a,<br />
stop: 0.5 #45a,<br />
stop: 1 #238 );<br />
border-bottom-right-radius: 7px;<br />
border-bottom-left-radius: 7px;<br />
border: 1px solid black;<br />
}</code></p>
<p>And here&#8217;s the <a href="/files/CoolProgressBars.ui">.ui file</a> you can open in Designer (right-click that link and choose &#8220;Save as&#8230;&#8221;).</p>
<p>Happy hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2009/10/13/cool-qprogressbar-stylesheet/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>LDS General Conference Podcast Updated for October 2009 Sessions</title>
		<link>http://thesmithfam.org/blog/2009/10/04/lds-general-conference-podcast-updated-for-october-2009-sessions/</link>
		<comments>http://thesmithfam.org/blog/2009/10/04/lds-general-conference-podcast-updated-for-october-2009-sessions/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 01:54:42 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[LDS]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=506</guid>
		<description><![CDATA[I added the October 2009 sessions to the podcast. If you&#8217;re already subscribed, you don&#8217;t have to do anything. iTunes (or other podcasting software) will download the latest sessions automatically.
To subscribe to the podcast, visit the LDS General Conference Podcast page.
]]></description>
			<content:encoded><![CDATA[<p>I added the October 2009 sessions to the podcast. If you&#8217;re already subscribed, you don&#8217;t have to do anything. iTunes (or other podcasting software) will download the latest sessions automatically.</p>
<p>To subscribe to the podcast, visit the <a href="/blog/lds-general-conference-podcast/">LDS General Conference Podcast page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2009/10/04/lds-general-conference-podcast-updated-for-october-2009-sessions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lock Free Multithreading in Qt</title>
		<link>http://thesmithfam.org/blog/2009/09/30/lock-free-multi-threading-in-qt/</link>
		<comments>http://thesmithfam.org/blog/2009/09/30/lock-free-multi-threading-in-qt/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 23:17:55 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code and Cruft]]></category>

		<guid isPermaLink="false">http://thesmithfam.org/blog/?p=472</guid>
		<description><![CDATA[If multithreading is challenging to get right in your applications, then lock-free multithreading is down-right killer.
This article won&#8217;t go into detail about lock-free algorithms, but instead I will offer a &#8220;poor man&#8217;s&#8221; method for crossing thread boundaries in Qt without using locks (no mutexes, no semaphores). At least, your code won&#8217;t have any locks. More [...]]]></description>
			<content:encoded><![CDATA[<p>If multithreading is challenging to get right in your applications, then lock-free multithreading is <a href="http://labs.trolltech.com/blogs/2008/10/22/a-never-ending-struggle/">down-right killer</a>.</p>
<p>This article won&#8217;t go into detail about <a href="http://en.wikipedia.org/wiki/Non-blocking_synchronization">lock-free algorithms</a>, but instead I will offer a &#8220;poor man&#8217;s&#8221; method for crossing thread boundaries in Qt without using locks (no mutexes, no semaphores). At least, <b>your</b> code won&#8217;t have any locks. More on that later.</p>
<p>For years, Qt has sported an easy-to-use threading library, based around a class called <b>QThread</b>. As of Qt4, you can use <b>QThread</b> to start your own event loops. This might sound somewhat uninteresting at first, but it means you can have your own signals and slots outside the main thread. The Trolls created a new way to connect signals to slots such that signals can actually <b>cross</b> thread boundaries. I can now emit a signal in one thread and receive it in a slot in a different thread. This is hugely useful when you want to, for example, integrate a blocking-happy library into your application. Here&#8217;s what I&#8217;m talking about in pictures:</p>
<p><img src="/images/qt-multithreading-1.png" /></p>
<style>
pre { font-size: 10pt; border: 2px solid #016; padding: 5px; color: white; background: #333; }
</style>
<p>Signals can arrive at any time from the threads, just like any other signal, and the code in the main event loop doesn&#8217;t know anything about multi-threading, locks, or condition variables.</p>
<p><b>An Example</b></p>
<p>Let&#8217;s say you want to integrate with a third-party library that blocks when you call its functions. Of course, if you call this library from your main event loop, your application will freeze while it&#8217;s running. That would be annoying, and you can give your users a better experience than that. Let&#8217;s wrap that library in a <b>QThread</b>. First, you need to declare a new QThread subclass, like this:</p>
<pre>class MyLibraryWrapper : public QThread
{
Q_OBJECT
public:
    MyLibraryWrapper();
protected:
   void run();
signals:
   void done(const QString &#038;results);
private slots:
   void doTheWork();
};</pre>
<p>The <b>run()</b> method is called automagically by Qt when the caller calls <b>start()</b> on your thread. This is similar to how Java&#8217;s <b>Thread</b> class works.</p>
<p>The <b>done()</b> signal is how the object tells you it is finished with its work. It emits a <b>QString</b> in this example, but it can emit anything you want (note that if you want to emit non-primitive or non-Qt types, you need to use the <b>Q_DECLARE_METATYPE()</b> macro, which we won&#8217;t go into in this article).</p>
<p>The <b>doTheWork()</b> slot is there to actually do the blocking work. It has to be a slot so we can put it to work <b>after</b> our event loop starts up (which you&#8217;ll see in a minute).</p>
<p>Now for the implementation of <b>MyLibraryWrapper</b>:</p>
<pre>
MyLibraryWrapper::MyLibraryWrapper() : QThread()
{
  // We have to do this to make sure our thread has the
  // <a href="http://doc.trolltech.com/4.5/qobject.html#moveToThread">correct affinity.</a>
  moveToThread(this);

  // This will do nothing until the user calls start().
}

void MyLibraryWrapper::run()
{
  // This schedules the doTheWork() function
  // to run just after our event loop starts up
  QTimer::singleShot(0, this, SLOT(doTheWork()));

  // This starts the event loop. Note that
  // exec() does not return until the
  // event loop is stopped.
  exec();
}

void MyLibraryWrapper::doTheWork()
{
  // Do the heavy-duty blocking stuff here
  // (simulated by a 5 second sleep for
  // this example)
  sleep(5);

  // When you're done, emit the results:
  emit done("First job's finished.");

  // And some more sleeping for fun
  sleep(3)
  emit done("Second job's finished.");

  // ...
}
</pre>
<p>To actually use this new class, all you have to do is instantiate it in your main event loop and connect it to a slot. Here&#8217;s an example. Let&#8217;s say you have a <b>QMainWindow</b> called <b>MyMainWindow</b>. This is how you would set it up as a result of a user button click:</p>
<pre>
void MyMainWindow::on_someButton_clicked()
{
  MyLibraryWrapper *wrapper = new MyLibraryWrapper();

  // This is the magic that tells the wrapper to
  // notify us when it's done. We use a QueuedConnection
  // to make sure Qt delivers the signal in a thread
  // safe manner
  connect(wrapper, SIGNAL(done(QString)),
          this, SLOT(wrapperDone(QString)),
          Qt::QueuedConnection);

  // This kicks off the wrapper's event loop by causing its
  // run() method to be called
  wrapper->start();
}

void MyMainWindow::wrapperDone(const QString &#038;results)
{
  // The wrapper is now done with its long, blocking
  // operation, and we didn't freeze the application.
  // Yay for us!

  qDebug() &lt;&lt; "Here are your results:" &lt;&lt; results;
}
</pre>
<p><b>Conclusion</b></p>
<p>Notice that none of our code uses a semaphore, condition variable, or mutex? Using <b>QThread</b> makes it super easy to wrap up libraries that need to block the event loop in a way that is transparent to the caller.</p>
<p>I claimed earlier that this code would be &#8220;lock free&#8221;. I can&#8217;t actually claim that is 100% true. I don&#8217;t know for sure, but I imagine that Qt&#8217;s internal event loop code does indeed use locks to pass signals between event loops. All I know for sure is that <b>this</b> code crosses thread boundaries without any locks.</p>
<p>Stay tuned for another article that shows how you can call setter functions on your threaded objects without any need for locks.</p>
]]></content:encoded>
			<wfw:commentRss>http://thesmithfam.org/blog/2009/09/30/lock-free-multi-threading-in-qt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
