Effective Qt in ruby (part 1)

As some of you might know, I’m working on a generic board game platform called Kaya. Kaya is a Qt/KDE-based application to play chess, shogi and variants thereof, and easily extensible to all sorts of board games (Go is in the works, for example).

Kaya is written in ruby, and I have learned quite a few things about writing non-trivial GUI applications in ruby while working on it, so I decided to share some of my experience and code in the hope that it might be useful to others, and possibly inspire other Qt/KDE developers to try out ruby for their next project.


Here is a list of what I think are the most important points that make programming GUIs in ruby so much more productive than in C++. I’ll leave out subjective arguments like “it’s more fun” or “it has a nicer syntax” because I think that the actual facts are more than compelling already.

Fast Prototyping

This is not specific to GUI programming. It is greatly aknowledged that ruby is orders of magnitude more convenient than C++ for throwing quick scripts together and in general for trying new ideas out. This turns out to be very important in a GUI context as well.

For example, it is very easy to write setup code for a single component of your application so that you can run it standalone and test it more efficiently. I have found myself resorting to this kind of trick very often, and it sometimes makes debugging a lot less painful. In C++, it would be unreasonable to write a new application skeleton (and alter your build scripts) just to test a new dialog you are developing.

Another example is “fancy logging“. If something breaks in the middle of complicated or highly interactive code, sometimes printing to the console or setting breakpoints doesn’t quite cut it. In these cases, it is a very good idea to hack together a simple but powerful visualization tool to show the internal state of the application in real time, and that is usually very helpful to identify the issue. Again, since this is basically throwaway code, ease of prototyping is very important.

Declarative GUI Definition

Ruby has very powerful metaprogramming facilities that make creating an embedded DSL extremely straightforward. I use a simple mechanism in Kaya that allows me to write things like:

    @gui = KDE::autogui(:engine_prefs,
                        :caption => KDE.i18n("Configure Engines")) do |g|
      g.layout(:type => :horizontal) do |l|
        l.layout(:type => :vertical) do |buttons|
                         :text => KDE.i18nc("engine", "&New..."),
                         :icon => 'list-add')
                         :text => KDE.i18nc("engine", "&Edit..."),
                         :icon => 'configure')
                         :text => KDE.i18nc("engine", "&Delete"),
                         :icon => 'list-remove')

I find it very convenient to define GUIs at this slightly higher level of abstraction, plus you have none of the boilerplate code typical of GUI construction in C++.

Defining GUIs in this way is so quick (and it’s easy to run them to immediately see the results), that it makes tools like Qt Designer completely redundant, in my opinion, except possibly when GUI design and coding are done by different people.

Using Closures for Slots

Take a look at this simple example, and try to imagine how many lines of code would be needed to implement it in C++:

win.display.text = "0"
inc = 1
Qt::Timer.every(1000) do
  win.display.text = (win.display.text.to_i + inc).to_s
win.button.on(:clicked) { inc = -inc }

Here win.display is a QTextEdit and win.button is a QPushButton. What the example does is count up seconds in the QTextEdit, and toggle between that and counting down when the button is pressed.

Pretty trivial, granted, but in C++ you would need to define two slots to do that, plus keep track of the direction in which you are counting by using a member variable. In ruby you can just use local variables without littering the parent scope.

Toolkit Independence

If you write your application in C++, once you’ve decided that you are going to use KDElibs (or Qt), that decision is pretty much set in stone. You can’t even switch from KDE to pure Qt very easily, and it’s so hard to support both environments in a single code base, to make it almost completely not worth the effort.

Of course, this might not seem that big of an issue, given that KDE is now a lot more portable, but not many people are running KDE SC on Windows or Mac at the moment, and if you want to reach as many users as possible, having a Qt-only version of your application is going to help a lot.

So Kaya can currently run on Qt-only as well as KDE. When in KDE mode, all the usual KDE goodies are running under the hood: KPushButtons, KDialog, K-everything, and of course KXMLGUI and all the good stuff that comes with it, but it can switch to plain Qt classes and a simplistic replacement for the XML GUI if you don’t have KDElibs installed.

Easier Automatic Testing

Dynamicity, mock objects, plus the whole culture of test-driven development that characterizes the ruby ecosystem make it a lot easier to devise automated tests for your application. Effective GUI testing is still basically an open problem in software engineering, so don’t expect it to be a piece of cake, but in my experience, you can definitely reach a considerably higher test coverage with ruby than with C++.

Trivial Extensibility Through Plugins

KDE really shines when it comes to making applications easily extensible via scripts or plugins, but it can’t compare with ruby. The distinction between plugins and user scripts is now nonexistent, and loading a plugin is just a matter of calling the ruby load function.

Creating a sensibly extensible application still requires careful planning, of course, but it’s a lot easier when you can directly expose application functionality to plugins, without creating tons and tons of interfaces for even the most trivial uses.

In Kaya, the use of a dynamically typed language turned out to be key: the flexibility required to make its plugins easy enough to write without knowing much about the application internals is pretty much impossible to achieve in a statically typed context.


Of course, like everything in software engineering, choosing ruby over C++ for GUI development involves a number of tradeoffs.


Everyone knows that ruby is slow. Painfully slow sometimes. That seems to be improving with 1.9, but ruby isn’t going to get faster than C anytime soon.

Now, the good thing is that its poor performance is almost always completely irrelevant. A typical GUI application is nowhere near being CPU bound, and the few computationally intensive parts are usually in the GUI library anyway. That said, if you have performance critical sections in your application, you are better off writing them in C/C++ and accessing them from your ruby code using the ruby extension API.

For example, Kaya includes a small C++ extension to supplement the missing blur functionality in Qt < 4.6. Writing it and integrating it was trivial.

Testing is Essential

Automated testing is very important for software written in any language, but for dynamic languages you simply can’t do without it. Untested code will inevitably contain silly typos and type errors which will cause it to crash in your user’s faces, or in the best case will make your testing sessions painful and slow.

So you don’t have any choice but to add lots and lots of unit tests, integration tests and the like. Units tests for pure GUI code are not really effective, useful or easy to write, but you can settle on smoke tests that will cover the most common mistakes and be pretty confident that no silly errors will pop up that a compiler would catch.


I hope this gives a nice overview of the benefits you can get by switching from C++ to a dynamic language for your KDE (or general GUI) development. I used ruby as the main example, because that’s what I have experience with, but most of the points probably apply to dynamic languages in general (python, perl, clojure, etc).

In the following posts, I will dig a little bit more into Kaya’s code to show the kinds of tricks that I employed to better exploit the advantages that I discussed, and offer even more ways to get the most out of ruby for GUI programming. Stay tuned!


5 Responses to “Effective Qt in ruby (part 1)”

  1. Anon Says:

    Really great article: thanks for sharing your thoughts! The kdebindings (and the kdebindings team) are criminally underrated, IMO 😦

  2. Marcin Kulik Says:


    Nice write-up. I’ve been also doing some Qt/Kde development in ruby (see here: http://github.com/sickill/kodr). Combo Ruby+Qt is so productive that when I think about writing some GUI app it immediate becomes obvious choice for me.

  3. saLOUt Says:

    Hi Paolo,

    which ruby version are you using?
    How do you manage to not run into troubles with threads (ruby threads vs. Qt/C++ threads)?

    Nice to hear that there are some serious KDE projects using ruby.
    I am going to take a deep look in the code – maybe I can help you.;)

    Best Regards,
    PS: probably already usefull for you:

  4. Effective Qt in ruby (part 2) « Paolo Capriotti Says:

    […] Paolo Capriotti KDE hacking and more « Effective Qt in ruby (part 1) […]

  5. x37v Says:

    Great posts! I have a gui project that I’ve been sort of dreading getting back to because of C++ and I think I’ll simply translate it to ruby, which I really enjoy working with.

    In a future post I’d love to see how you implemented your blurring functionality. I’m specifically interested in seeing how you access the QT functionality from your ruby-c/c++ extension. Maybe this is completely trivial? Either way it would be an assurance to see how it is done.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: