I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009


Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Monday, October 22, 2007

Update: GTK vs. QT

22 July 2008: Now I take more into account than mentioned in these two posts. Please, this was a long time ago, do not comment on it. I use both GTK and Qt today, using each for different purposes that accommodates to their strengths.

Apparently, Digg put my GTK vs QT article on the front page of their Linux/Unix section (giving me about 1500 visits today) and I read the various comments replying my post (both here and on Digg), and I apologize if I made anyone feel uneasy about this comparison. I know that a Hello World program may not show much, but how do all beginners learn to program with a certain API or programming language? Hello World! I'm a beginner to Linux GUI programming APIs, so the Hello World programs give me a glimpse as to what the API/language is like. 

From the looks of this simple Hello World application in both QT and GTK, I became biased over QT. However, I must also note that a thought came to me that because GTK's code tends to be longer, programmers might have more control of their code because of that. 

Also, due to my Visual Studio programming roots, I found that QT's syntax seemed more familiar to me in some places, like hello.resize(100, 30); I'm really used to the dots.

Again, I apologize for my unfair examination of the two APIs and hope you can understand my viewpoint for being biased toward QT. But this is just like comparing apples and oranges. Whichever one you choose depends on your taste.

If you'd like to feel happy, read a list of improvements in Gutsy Gibbon over Feisty Fawn. Or, vote on which API is your favorite in the sidebar.


amosbatto said...

Gee, Qt really sucks because it takes 13 lines of code to say "Hello World" when GKT+ can do it in 119 characters:

#include <gtk/gtk.h>
GtkWidget *m = gtk_message_dialog_new(0,0,0,0, "Hello World");

Obviously GTK+ is better because it can say "Hello World" in less lines of code!

As several commentators have already pointed out, this comparison of "Hello World" code snippets is a meaningless comparison of Qt and GTK+.

To be fair to the UNIX geek, he was just copying the "Hello World" sample found in the GTK 2.0 Tutorial, but that code snippet wasn't designed to be short. It was designed to teach the beginner how to make a main window and add a button and link callback functions to widgets and how to display the widgets. There are many obvious ways to shorten the GTK+ code sample, but the purpose of the code was to demonstrate various important points about GTK+.

Most other people wouldn't start off with such a complex example. For instance, Andrew Krause's book _Foundations of GTK+ Development_ starts off with a "Hello World" program which is only 11 lines long.

To be fair to UNIX geek, it generally takes more code to do things in GTK+ and Qt, so his main point is valid. I've seen the estimate that it takes 30% more code to write a GTK+ program than a Qt program. Still very few GTK+ programmers hand-code their interface anyway. They use Glade to design the interface and then only write the callback functions to connect to the XML interface designed by Glade.

Personally, I think that you should choose your toolkit for other reasons than the number of lines of code. For instance, GTK+ generally takes less memory and processing power to run than Qt. If you are a C programmer and don't care for the complexity of OOP, then GTK+ is better. GTK+ doesn't have any licensing costs in Windows like Qt and GTK+ runs on more platforms. Plus GTK+ is supported by a bigger community and has the backing of Novell, SUN, and Red Hat, yet isn't controlled by any one company. In other words, GTK+ is garanteed to be a technology which will exist for many years to come. Furthermore, GTK+ has excellent graphical functions, since it is the toolkit for the GIMP.

On the downside, GTK+ is poorly documented. For instance, there are almost no examples in the documentation, and the Tutorials to learn GTK+ are hard for the beginner to follow. There is currently only one book available which covers GTK 2.0 which is Andrew Krause's book. All the other books on GTK+ are based on version 1.2 and are extremely outdated and should be avoided, since GTK 2.0 is very different from GTK 1.2.

Also, GTK sometimes makes simple tasks extremely hard. I don't know Qt, so I can't really compare. I have heard that Qt is easier to learn and has a more logical design, whereas GTK feels like it was cobbled together from various different parts and projects.

I have also been told that wxWidgets offers all the benefits of Qt, but also gives you all the freedom of GTK+ in terms of licensing, but it seems to have less mindshare than either one. Considering that wxWidgets has been around since 1992 and runs on more platforms than both GTK+ and Qt, it is surprising that it hasn't attracted more attention.

At any rate, don't decide to use a toolkit because of some pointless comparison between "hello world" programs.

Anonymous said...

I see the main differences here:
- Gtk+ is LGPL and *implies* lesser costs for proprietary projects.
- Gtk+ is C and even gtkmm is more of an C++ wrapper than design with object orientation in mind.
- Qt is a bottom-up OO designed framework.
- The memory footprint of Gtk+ is not significant (and negliable with gtkmm) lower.
- Performance wise you will find no big differences, in some areas Gtk is a bit faster and in some Qt is.

...but most important:
- Gtk+ is mainly a GUI toolkit, where as Qt is a complete consistent framework. You can easily connect HTTP events to GUI elements, fill forms with results from a database query or build and interactive visualization of large datasets. There is barely nothing you can do with Gtk+ without taking other libraries with different API conventions and designs, be it libcurl even Cairo.

Victor said...

1. It's not an issue anymore. Qt4 is superior.
2. GTK's API is only OK with people who agree to use gtk_message_dialog_new() instead of "new QMessageBox". I mean, seriously? I don't care what your personal preference is. It's 2008 and you should wake up. OOP, if not abused (and you can abuse C too), provides great functionality, far superior than C. Qt API is cleaner, easier to understand and INTUITIVE. Nobody mentions this, but gtk_message_dialog_new or gtk_new_message or gtk_new_dialog_message seems similar, don't they? Compare this to QMessageBox::information() or QMessageBox::error() - it's clean, clear, and you can easily reconstruct the class/method name from your memory, if you have seen it at least once. No need to even go to the docs!

Those of you who look at some code and think : 'Oh, well, they look the same' are clearly not used to WELL DOCUMENTED CODE. Documentation should be one of the most important factors when choosing an API. You ARE going to get stuck once, without knowing how to accomplish something in your app. What do you do then? In Qt, the documentation is excellent. I do mean excellent. Its the kind of documentation that makes it useless to buy a book on Qt - it's as good as book!
And it's not like your usual Wiki pages - lumped together like a big pile of crap. It's structured, it's organized - it's taken care of.
Qt must be the best thing that happened to C++. I would recommend Qt to anyone who is doing C++ GUI applications.
If the open-source community produced code that is at least half as good as Qt code, then we wouldn't hear the expression 'you get what you paid for'. And unfortunately, for most open source apps, you really get what you paid for. But rarely, something comes up and you think 'My God, this is open source?'.
I have come to respect the folks at Trolltech, now working for Nokia, for their quality software and for proving that Open Source software means software written by talented and devoted programmers, not those feature-driven, half-assed programmers who keep developing and releasing these alpha-staged programs and then arrogantly hiding behind the 'Open Source' idea to justify their lack of professionalism and the billion bugs in their software.
Just take my word for it, I have tried lots of toolkits outthere, and Qt is as good as a toolkit can get.
If you don't believe me, at least try it and judge for yourself.
In any case, make sure your application concentrates on quality, not quantity!

Anonymous said...

Well, I have to say that the comparison of lines of code needed for "Hello World" is a bad comparison. It hardly takes any coding to do "Hello World" in MFC, does that make MFC good?I personally like QT4 better that GTK2.

I started looking for a good cross-platform GUI toolkit when I was taking a class on C++. My teacher was using MFC, but I hated the idea of using a Windoze only toolkit. So, I began looking for alternative; I first found GTK2- I found that old, obsulete, not addaptable to other OS's; I then found wxWidgets- but found that very unsupported and confusing. Then I talked to another geek in my class who used linux; he recommended QT. So I downloaded the latest version of QT for my system. I found QT to be easy, good-looking, and very well documented. Within week, I had mad a calculator program; a tic-tac-toe program with AI, 2-player mode and HS list.

One of the main things I have against GTK is that though gtkmm is available for use with C++, GTK is written in C- an outdated technology.