O'Reilly Network    
 Published on O'Reilly Network (http://www.oreillynet.com/)
 See this if you're having trouble printing code examples

Boudewijn and Cameron Argue for Qt

by Cameron Laird and Boudewijn Rempt

Related Articles

Qt Attracting Interest Among Application Developers

Visual Design With Qt

Editor's note -- Seldom do I run across an article on application development that is just plain fun. I have one here. There are four characters in this story talking about the Qt authoring environment: Cameron Laird, Boudewijn Rempt, Thomas, and Paul. Cameron and Boudewijn are real life developers. Thomas and Paul are composite characters that represent real views. The discussion among them is lively and informative.

Last Wednesday Thomas, a fellow hacker, came by for a visit. He's heard our praise of the Python language and Qt graphical user interface (GUI) toolkit. Thomas, though, is a doubter, one who needs to see for himself. He arrived with a demand to be shown "what all the fuss is about." So we did:

Thomas: Well, I'm a Windows developer mainly. That's where the money is, isn't it? Why should I use a cross-platform GUI toolkit when there's a perfectly good native one, and that's MFC (Microsoft Foundation Classes).

Boudewijn: True, you are a Windows developer ... for now -- you won't be one for ever. Even Nathan Myhrvold, chief technologist at Microsoft until last year, thinks that Windows will have been replaced by another system in five years time. You might want to grab the opportunity to have your app running everywhere.

Cameron: That's not all. Even though MFC is native, we think we make better Windows applications with Qt than if we were using MFC. Frankly, it looks to me as though Microsoft is coming to a similar conclusion; they appear to be de-emphasizing MFC, and certainly not investing more in it. Qt's design is a strong one. Signals and slots (the "callback" mechanism in Qt) make for safer programming than MFC's hooks. Qt is savvy about Unicode from the ground up; internationalization simply works the way it should.

Boudewijn: It's hard to show you a side-by-side comparison of Qt and MFC, because MFC is so thin a layer on the C foundations of Windows - Qt is C++ from the ground up. In contrast with MFC, it's very easy to create a reusable component that can work as well as a document view as in a dialog window:

class complicatedWidget(QWidget):
  def __init__(self, *args):

class aDialog(QDialog):
  def __init__(self, *args):
    self.layout=QGridLayout(self, 1, 1)
    self.widget=complicatedWidget(self, "dialog view")
    self.layout.addWidget(self.widget, 0, 0)

class aWindow(QMainWindow):
  def __init__(self, *args):
    self.view=complicatedWidget(self, "main view")
    self.setCaption("Main Window")

The only difference is that a main window has a built-in layout manager.

Thomas: Hmmm. Well, maybe ... Suppose I do decide to go cross-platform; shouldn't I use Tkinter?

The IDEL treeview

The IDLE treeview.

Boudewijn: You could. It's quite good ... But maybe just a little old-fashioned. It's simply missing several of the features I know you'll expect. If you want to use a treeview, you have to construct one yourself. Look at the IDLE (IDLE is the Tkinter development environment written in and bundled with the standard Python distribution) treeview, for example -- they're still working the bugs out of that one, and it's currently at about 500 lines of code.

I will say this, though: Whatever toolkit you choose, buy Grayson's Python and Tkinter Programming. It's a delight to read, and handles all aspects of GUI design.

Thomas: I've looked a bit at the market for Linux, and Gnome sounds good. So shouldn't I develop two version of my apps, one for Windows and one for Gnome?

Boudewijn: You might like Gnome -- but will your users? Not everyone is drawn to the dark side of interface design. What I mean is that the Gnome people designed for looks and theme-ability before they tried to achieve stability and functionality. Standard Gnome looks cool, but not very businesslike.

Cameron: I like the Gnome guys: They're imaginative and energetic. They're just not done, though. Qt is far more mature. GTK (Gnome's toolkit) copies Qt's signals and slots mechanism, but really doesn't do it as well.

Thomas: What's this stuff about the license? I just read that I've got to pay an enormous amount of money for the Windows version of PyQt! That's not open source!

Boudewijn: That's not entirely correct. To generate a PyQt application for Windows, you do need to buy a developers license of the main Qt library for Windows. That'll set you back $1550 USD.

PyQt itself is free. In fact, if you want to distribute a compiled version of PyQt for Windows with a Qt run-time dll, there's nothing to stop you. Be the first to sell a commercial Python/Qt development environment for Windows!

As for open source, you're half-right. You can't use Qt for free on Windows, but the Unix/X11 license is certified Open Source TM.

Thomas: Why are we even talking about PyQt? Qt itself is starting to interest me, but PyQt is only version 0.12. That's deep alpha -- as steady as strawberry jelly!

Cameron: Phil (Thompson, the originator and maintainer of PyQt) counts differently than most of us. For him, version 0.10 followed 0.9. Anyway, beyond that, he's careful with the software he puts out. You can trust PyQt. It's stable.

Boudewijn: It's very, very stable. There are bugs, of course, but they are quickly fixed. In over a year of PyQt development, I've had one serious problem that took a while to fix -- and I could still work around that one.

Thomas: KDE doesn't run on Windows, though, right? What's the point of using Qt then?

Cameron: This takes some explanation. "KDE" means about four different things. The acronym is for "desktop environment," which is what Unix people used to call a "window manager," plus more. KDE is also an application development framework and an office application suite. KDE stuff is all built with Qt -- as the KDEers explain their decision, "It's the best toolkit for Unix."

Boudewijn: It is a bit complicated. Qt itself is available both for Windows and for Unix/X11. So's PyQt. Apps written in PyQt run unmodified on both platforms, too. However, if you want nice KDE extensions, like extra-de-luxe widgets, components, or integration with the KDE desktop, you have to use a second set of bindings: PyKDE. PyKDE depends on PyQt.

Cameron: ... which is fine, of course. PyKDE needs the full KDE API (application programming interface), though, so PyKDE only works where you have KDE, which means only for Unix with X11.

Boudewijn: What's nice is that it's quite easy to write an app that integrates with KDE when it's available, but degrades gracefully to pure Qt if KDE isn't present. I can show you how.

#!/usr/bin/env python
import sys, os

from qt import *

  from kdecore import *
  from kdeui   import *
except ImportError:

class MainWindow(MainWin)
  def __init__(self, *args):
    apply(MainWin.__init__,(self,)+ args)

def main(argv):

  app=Application(sys.argv, "kpybrowser")

  return app.exec_loop()

if __name__=="__main__":

At this point Paul joined us. Paul has developed Unix applications for ages. He's now looking longingly at the market Linux offers.

Paul: I heard what you told the youngster. Look at me: I've got an enormous amount of Motif code and experience. Why should I switch?

Boudewijn: I honestly don't know. Should you switch? Do you still believe Motif has a future? When a commercial organization opens up a bit of legacy code, as the Open Group just did with Motif, or Borland with their database, I figure it's often the beginning of the end. ... It's better than keeping legacy stuff in a safe, but it's not promising.

Paul, look me in the eyes and tell me truthfully: Are you still having fun developing with Motif? Maybe you owe it to yourself to have some fun. Just try Python and Qt for a small personal project. I know I never had as much fun programming as I have now with PyQt ...

Paul: What interests me is Unicode. Using Unicode with Motif is a real problem -- it has been solved, more than once, but never really satisfactorily. I'm bit jealous of Thomas here, who has excellent Unicode support in Windows.

Thomas: I wish! It really only works for NT. Unicode for Windows 95 is ridiculous. I don't worry about it, though. Why should I use it at all? English was good enough for Saint Paul, so seven-bit ASCII should be good enough for you, Paul.

Boudewijn: Qt offers advantages for both of you. With Qt you've got full Unicode support both on Unix/X11 and on Windows 95/NT/2000. And don't think you don't need it. Even people who don't know an aigu from an umlaut now expect their applications to keep em-dash and en-dash straight, not to mention hyphens. If you do mathematics, you want all those mathematical symbols, don't you? Anyway, more and more of the world is computerizing. Do you think the Indians want to give up their devanagari script as they come online? Unicode is our best chance for a universal character set that everyone can use to exchange data. Grab that chance! I've got a nice little example of a Unicode-aware character selection window on my Web site, for example.

Cameron: I'll say it again: This is an example of how Qt is more native than native toolkits. MFC might be the default choice for Windows, but it doesn't give you the consistency of features between W95 and NT that Qt does.

Paul: I've got a technical question. Qt is a C++ library, right? I don't want to develop in C or C++ anymore -- it takes far too much time. So Python suits me fine. But how does PyQt handle C++'s pesky overloaded methods?

Boudewijn: Just fine. Phil has written an intelligent wrapper library that handles overloaded method selection based on the types of the actual parameter values present in the function call. Within Python, you can't create overloaded methods yourself, but there's no problem using the C++ overloaded methods. Look at this bit of code, where we first create a listviewitem without a parent, and then one with a parent:

    listItemParent=QListViewItem(self, "Parent")

Cameron: Phil told me, "I always think of myself as the author of SIP." SIP is his general-purpose system for binding together C++ and Python objects. The point is, he's done a lot to make it work properly. If you're anything like us, you'll find it easier accessing Qt through Python than with C++.

Paul: I've heard Qt is European. Is that right? Is it more popular in Europe than in the United States?

Cameron: Qt is the product of Trolltech, which is a Norwegian company.

Boudewijn: Likewise, a large amount of the KDE effort comes from Europe. The first Qt book was in German, and it has been translated into Japanese. I'm a European myself; I don't really know how popular Qt is in America, but it's pretty popular in these parts.

Cameron: North Americans are relatively fonder of GTK. It's a major religious issue for some people. There are plenty of Qt users hereabouts, though. There's a little suspicion around the world that U.S. software tends to be buggier and/or pricier. What's most certain is that Qt is a good Unicode citizen, and that's a big win in many places. With Python 1.6 supporting Unicode well, it makes PyQt a very attractive combination for internationalized application development.

Paul and Thomas: Well, we'll give it a try. What's the current release?

Boudewijn: The latest release of PyQt/PyKDE is 0.12. The license is no longer GPL (General Public License), but similar to the X11 license. A few bugs were fixed, a few functions added. But the most important new feature is the addition of the themes.py script. Yes! Full theme-able applications written in pure Python! Who doesn't want his app to look like it's crafted from expensive teakwood ...

Cameron: Before you go, I want to strongly recommend you read Trolltech's Qt documentation. It's one of the best features of the toolkit, and equally applicable if you're a PyQt developer. I think you'll like what you find.

Cameron Laird is the vice president of Phaseit, Inc. and frequently writes for the O'Reilly Network and other publications.

Boudewijn Rempt is a senior developer with Tryllian, a mobile agent company. He has worked with PyQt since its inception.

Related Articles

Qt Attracting Interest Among Application Developers

Visual Design With Qt

Discuss this article in the O'Reilly Network Forum.

Return to the O'Reilly Network Hub.

Copyright © 2009 O'Reilly Media, Inc.