Dec 22, 2009

Qt

It's been a while since I did any GUI development, but these days I've been working on a little project for a friend of mine. I decided to check out Qt, which is the main GUI library used for KDE applications. It's one of the main GUI toolkits for Linux apps that I still hadn't tried and cared to try (I haven't tried Tk, but I'm not really interested in trying it because of aesthetic reasons).

After a while of working with Qt, I have to give it two thumbs up!
Here's some of the highlights:
- "Garbage Collection" - it's not real garbage collection as far as I know, but the memory for GUI widgets is managed within Qt, so you don't have to worry about deleting them. It isn't full garbage collection because you still have to clean up your other stuff, but it's a nice to have.
- The meta-object system. Qt doesn't actually use real C++, it uses a slightly extended version which it converts to standard C++, and then compiles. This gives it the really nice messaging system that it has called slots and signals. A signal is a method that you don't define yourself, you just define a section in your class called signals:
class MyWidget : public QWidget {
...
signals:
void opened();
void closed();
At some point during your program you can emit a signal like this:
emit opened();
This makes your object send out the opened() signal to anyone who cares to listen.
How do you listen then? You use a slot. A slot is just like a regular method, except you can attach a slot to a signal. For example, a button in an app would send out the clicked() signal when you click on it:
connect(myButton, SIGNAL(clicked()), this, SLOT(myButtonPushed()))
This line would make it so that whenever myButton's clicked() signal is sent, the myButtonPushed() method on whoever "this" is would be called. You can even connect signals to other signals, so that when one signal goes off, it triggers another signal. This is handy for creating widgets that have subwidgets, but you don't want to reveal the subwidgets to other classes.
- The tools - You'd think that if there is this meta-compiler that has to run before the C++ compiler, the Makefiles for Qt would be nasty. And they probably are. However Qt provides you with a Makefile-generator so that you don't actually have to touch the darn things. You just go:
qmake -project && qmake
Running this from your code folder sets up a Makefile for your project so that you don't have to.
There is also an IDE for Qt development called Qt Creator. I haven't tried it because I'm not quite ready to stop using Vim, although supposedly Qt Creator has a Vim-mode so it might be interesting to try...unfortunately though it seems that to build Qt Creator you need Qt 4.6, which is not available in the Ubuntu repos just yet. Darn. Oh well, you can just use the qtcreator package. I'll try this out and let you know what happens.
- Aesthetics - the default widgets in Qt are appealing. They don't look like blocks, the fonts are well rendered, and animations are subtle/smooth. I know many developers don't actually care about aesthetics, but I do and this is a big win for me since I'm not very good at making things pretty myself.
- Cross-platform - my code can compile under Windows just fine, and supposedly under Mac as well. In fact Nokia advertises that it can run on Symbian as well, not that I need that very much.
- Cross-language - I'm using C++ for this, but you can write Qt apps in Java (and therefore any JVM language like Scala and JRuby) and Ruby, and probably more but I haven't bothered to check others.

So yeah, if you have the need to write a GUI-based app, give Qt a shot. It sure beats then hell out of GTK!

3 comments:

Alexandra said...

Nice post. Thanks! :)

Unknown said...

Interesting - I am finding that Qt is speeding up development on some things, but slowing down (or at least not improving) development on other things. Am looking forward to better documentation for 4.6 as well :)

But nice post!

Rob Britton said...

Alexandra: Glad you liked it!

Niels: What do you find it slows you down with? I'm still pretty new to Qt so I'm wondering what kind of speedbumps lie ahead.