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

featured

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

clickable portals

Sunday, October 21, 2007

GTK vs QT

UPDATE: PLEASE READ THE FOLLOWUP. THANK YOU!

This topic was once removed for its controversial nature. Please do not read this article if you feel offended by the examination of the two APIs. Read the followup (link just above this) for my reasons on such a biased viewpoint. Due to common demand, it has returned.


~~~~~~~~~ORIGINAL ARTICLE~~~~~~~~

Note: I did not try to make GTK look bad; both these programs are from the official tutorials for each API.

When it comes to developing GUI applications on Linux, people either will program in GTK or QT. GTK has no formal support and you can develop any time of application with it for free. QT, on the other hand, has an "open source" version, in which you must agree to make all programs you write with QT open-source. If you want to write commercial applications, you have to shell out some cash (of course, Trolltech provides commercial support).

So, from the looks of this, you'd most probably want to go with GTK. Wrong! QT makes programmers more productive that GTK. Compare a simple app where there's a small window on the screen with a button saying "Hello World" on it:

GTK (57 lines)

#include
static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}

static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");

return TRUE;
}

static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}

int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);

g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);

gtk_container_set_border_width (GTK_CONTAINER (window), 10);

button = gtk_button_new_with_label ("Hello World");

g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (hello), NULL);

g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (window));

gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);

gtk_main ();

return 0;
}

QT (13 lines)

#include
#include

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QPushButton hello("Hello world!");
hello.resize(100, 30);

hello.show();
return app.exec();
}

Again, both these programs do the same thing. With QT, less code means more productivity and ability to code more in a shorter period of time.

Of course, if you don't like C (GTK) or C++ (QT, the one I'm sticking with), you can always choose Python, Ada, PHP, etc. GTK and QT are just APIs, not programming languages.


Before posting your comment, please take into account what was said at the first line of this post and the followup (link at top). Thank you.

22 comments:

Anonymous said...

But Glade will reduce that to the amount of lines to what it is in qt.

Anonymous said...

First of all, your GTK+ code really sucks, I just want you to be aware of that. Two, if you're spending your day mucking around with pure widget manipulation, you're going to fail. Both Qt and GTK+ have application design frameworks for that, which let you get straight to the part of your application you need to code. Thirdly, I managed to do your GTK+ example in 8 lines of Python. How's that for line-over-line productivity?

Anyone who's counting lines as "how efficient" they are as a programmer needs to get a life.

Anonymous said...

Plus, QT is freakin' ugly. It makes your app look and act like it was designed to run on Windows but was ported to linux. Barf.

Dan said...

Ha! You got pwned!

Mikey said...

Coding, I leave that to my programmers, while I publish website related news and reviews on http://www.WhichWebsite.com

Zimmy said...

I put in my vote for Qt. Not because it's less code, I'd use the right widget set no matter what toolset i thought was right.

Qt has "native" looks to each OS. Gtk+ has it's own theming system, and it's ass ugly on Win32. Also, the Gtk+Quartz port is highly unstable, let alone drawing widgets with Carbon/Cocoa.

Qt has heavy licensing restrictions, which I hate, but they have a skilled team of developers, which work for Trolltech. Not saying that Gnome's devs are not skilled, but it's less organized than Trolltech.

russ said...

If QT wasn't so fugly then it might be worth it.

Anonymous said...

What we need is a completely new player in the game, made out of assembly language! >:3

Anonymous said...

You obviously showed a lot of code to make gtk look bad. I am a QT programmer and your comparison really sucks. Not even sure what was the point of this post. Perhaps to get people to visit your site. Sad :(

bicchi said...

I really hate to compare things but in all honesty Gtk is based on C and QT on C++. So in all fairness here is the gtkmm version of the program in c++.
By the way, 13 lines also.
Compile like this:
g++ main.cpp -o main `pkg-config gtkmm-2.4 --cflags --libs`

#include gtkmm/button.h
#include gtkmm/window.h
#include gtkmm/main.h

int main(int argc, char **argv) {
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Button button("Hello World");
button.show();
window.add(button);
Gtk::Main::run(window);
return 0;
}

Wedge said...

Never take a post down cause people don't disagree with it. Its your view, if other people have a problem with it, tell them to not read the post, or go F*ck them selves cause they are self centered a$$ holes and only think of themselves. That's my opinion.

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>
main(){gtk_init(0,0);GtkWidget*m=gtk_message_dialog_new(0,0,0,0,"Hello World");gtk_dialog_run(m);}

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...

Gnome may costs me a some more cents than KDE Tool Kit for building my apps. But I can use a (short GCC app's builder, similar to Kommander) + (many and many commands bash programs on any Linux) + (Firefox plus extensions + Dojo ToolKit) to get a very good solution to my daily beats practical problems.

Troll's may continue ahead to hell!

David Lee said...

We can discuss this the wiki-way at http://www.wikivs.com/wiki/Qt_vs_GTK.

I already put up some of the commonly found info.

Please correct if any of the information is not accurate. Thanks.

sentencedtolife said...

Has anyone looked at XVT? It does use native GUI in any of 10 (12?) operating systems...www.xvt.com

It works for both C and C++ GUI development.

Anonymous said...

The program in Vala
(http://live.gnome.org/Vala):

using Gtk;

static int main (string [] args) {
Gtk.init (ref args);
var window = new Window (WindowType.TOPLEVEL);
var button = new Button.with_label ("Hello World");
window.add (button);
window.show_all ();
Gtk.main ();
return 0;
}


$ valac hello.vala --pkg gtk+-2.0

Flame said...

This article should never have been removed, rather, comments should have been disabled from the start. Any comparison of Qt and Gtk is bound to be biased, so live with it.

Also, some of the comment posters in this thread extended the flame war from Qt and Gtk to KDE and GNOME. Nowhere in this article were those mentioned. This is an article about development toolkits, not desktop environments.

Someone else mentioned that Qt is ugly, and that it makes the software look and act like it was designed for Windows. Well, Linux was *not* designed to look different than Windows. Neither was it designed to look the same as Windows. Linux was designed to allow people to be free, including freedom of the way you set your desktop up, the freedom to be different, the freedom to make your desktop look exactly the same as Windows or Mac, and the freedom to use one or both or none of QT and GTK.

In the free software world, no free software is better than another. FREE software, FREE as in the freedom to choose.

SneakyWho_am_i said...

I think it was a great post. The code was useless (however official) and the post itself was almost meaningless to me, but look at the great discussion! Some people may have started irrelevant talk of KDE and Gnome, but I'm interested in a nice cross platform widget/graphics library - it's hard to choose between QT and GTK. Now I remember that I can throw them both away and go for something obscure like wxwidgets instead.

For a beginner that sot of information is not unwelcome.

Hugo said...

I am currently using GTK. I have used wxWindows and Qt.

I dont understand Why GTK uses TRUE and FALSE macros, instead of true and false? why use gbollean and gchar instead of bool and char? C standard is so beautiful and much more intuitive!

GTK Macros make the code so much more complicated and add compilation errors hard to fix (making you search for the macro code).

Documentation is poor, many times the documentation for some functions does not exist.

GTK is full of bugs, many widgets (like the tree view/model) don't work as it should ... and the quality of code is so ugly.

That's why I dont like GTK !!!

Alexander said...

Even stupid Bill Gates said it:

"Measuring programming progress by lines of code is like measuring aircraft building progress by weight."

Even though that's like saying:

"If my software is buggy, bloated, and a resource hog, I'd call it feature-full, and if it can't do a single simple thing right and one feels it's easier to do it without the computer, than I'd call it reliable and lightweight."

Note - Windows, to my opinion, both is bloated and cant do a thing right.

Alexander said...

And by the way, if you ask me, Trolltech are right for squeezing proprietary software developers - and they squeeze them VERY WELL - 3000 Eur for every single developer working for deploying on every single OS (and that's only for desktop ones, not embedded!)

JPerez45 said...

QT has nice features but is not perfect, by design it uses native widgets (and that was a path JAVA learned is not so good) also it lacks of a distributed component system like Microsoft's COM and ActiveX. GTK has Bonobo which is a CORBA-like implementation that helps to distribute portions of code with specific functionality, so you can reuse more components in GTK than QT and that's more productive.

At the end you should at least try both and then use the one you feel more alike with your projects and personality.