Tag Archives: Qt

Getting Started with Qt II: Qt Development Tools

In my last post, I tried to convey some of my excitement about the recently-LGPLed Qt framework for multiplatform develpment. I can list off great features all day and still not cover everything, but more importantly, Qt is great because there’s almost always something great about it that feels like it was built just for you. I think the best way to experience this is to take some time to sit down and play with it yourself, so I want to help you do just that.

Luckily, the 4.5 release has made it even easier to do just that with the introduction of the Qt Creator IDE. Binaries are available for Windows, Linux, and Mac OS X, so if it runs on your desk, it probably can run Qt Creator.

Qt Creator is included with the Qt SDK, which can be downloaded here.  I’ll cover integration with other IDEs like Visual Studio 2008 and Xcode in time, but for now, Qt Creator is the fastest way to get up and running to give things a try.

Rather than a typical hello world program, which is covered well elsewhere, I’d like to jump into some of the practical power of the framework by building a simple JavaScript interpreter. Think of it as a powerful version of the traditional calculator example.

Here’s a (terrible, hand-drawn) sketch of our goal:

qt_tut_thumb001

We have two major elements: the log area and a single-line text entry area with an “execute” button. On the back end, whatever you type into the text box will be executed by a script engine and the results are displayed in the log.  We’ll add some features as we go along to make it nicer.

We won’t be writing our own JavaScript engine.  Instead, we’ll leverage Qt’s QScriptEngine class so we can focus on adding features that are useful for us.  Have a look at that API and think about how you would implement this application.

Rather than publish this tutorial all at once, I’m going to post this in parts.  Think of it as a “Qt in 20 minutes a day” sort of tutorial.  If there are any topics you want to see covered or in expanded detail, let me know in the comments.

Starting with the Qt Framework

Although I’m far from using Linux as my main OS, I have started using it side-by-side with OS X on my desktop.  It holds a lot of promise, particularly seeing a sort of renaissance associated with the rise of Ubuntu.

On the other hand, the flexibility Linux gives to users and developers can be intimidating to deal with coming from more rigid worlds like those of Mac OS and Windows.  Regardless of what you feel about .NET and Cocoa, they’re at least consistent. Even with the improved standardization of Linux distributions and the stability of their library APIs, it’s a far cry from the safety of the world of the two more popular systems.

For several years now, Qt Software (formerly Trolltech, now owned wholly by Nokia) has offered a consistent and full-featured C++ toolkit not only for Linux but across Mac OS X and Windows as well.  It is offered in both commercial and, more recently, LGPL distributions.

Obviously a consistent, OS-native, non-bytecode-based toolkit is very desirable for performance reasons, but Qt makes another excellent addition to C++ that makes development a joy:  slots and signals.

It’s an implementation of the Observer design pattern that’s excellent for asynchronous operations that are common in GUI applications.  Importantly, it allows run-time registration of receivers and multiple-target signals, which we say are emitted from an object and received by the registered slot of other objects.

For example, suppose we have set up a button in our Qt GUI named “myButton” which emits, aptly, “clicked()” as a signal when it is clicked.  We would like to call the void function “calculate()” of the object “myCalculator” when myButton is clicked.  We can register this relationship with this single line of code:

connect(myButton, SIGNAL(clicked()), myCalculator, SLOT(calculate()));

Obviously, this isn’t the most powerful example of this mechanism since it’s merely a one-to-one relationship, but realize a signal may emit to several slots, and slots may receive signals from multiple objects, and you start to see how code can be simplified and made more intuitive.

More to come.