QtRuby on Windows (Qt 4.6.1 and Ruby 1.8.7)

I've been wanting to learn how one binds a GUI Toolkit into Ruby for quite a bit, I've now finally acted upon it. I first investigated possibilities by looking into this list. Shoes seemed like a nice fit :) yet it lacked the punch I was gunning for -- also, being a fan of drag and drop Widget arrangement and auto code-generation, it was obviously a tad under-par. Seeing as my first intership a bit back entailed developing a small GUI in Qt (and liking it !), I wanted to reembark on the Qt ship.

It's indeed a cute GUI framework, very well documented (books available as well), free and open-source. It's also powerful and feature-packed, gaining interest since its launch and being used in many projects (last.fm Scrobbler, Google Earth, VirtualBox, KDE and so on -- just do a search for 'Qt*.dll' on your HDD and see :) ) with its core being developed by Nokia. 

Having passed the introduction (yay!), I'd like to share a non-painful way of installing Qt bindings for Ruby (QtRuby) on Windows (tested on Windows 7 Professional x64, I guess the method should work on XP as well).

I downloaded the Qt SDK 1.1 at first, that seemed to be a bit overkill, Maemo emulator and what not. To get started, download the latest version of the Qt libraries (here is a direct link to download v4.7.3). Also grab a version of MinGW with gcc 4.4 (direct link here), since the new installer doesn't provide a download link, only requesting the install folder for MinGW and complaining if it's not a good version :).

I followed a nice article on what needs to be installed next: a custom-brewed installer for Windows, of course! Always a pleasure to have to monkeypatch something to make it play well with ole' Win32. Download via direct link and install. Choose not to include the Qt folder into the PATH, that will interfere with what we're gonna (monkeypatch) in the next step. Also provide the path to your Ruby's bin folder (tested with Ruby 1.8.7 p330, e.g. the last patch for the 1.8.x version -- as the title states, the current version of the QtRuby library which makes all the magic of connecting Qt to the Ruby world does not support Ruby 1.9.x).

Now it's time to get the aforementioned QtRuby library, use the direct link . It's also available as gem form, I guess it should also work when installed via rubygems, I've chosen to do a manual install. Having downloaded the QtRuby gem for Win32 (latest version: 2.1.0), run a 'gem unpack' on the qtruby4-2.1.0-x86-mswin32.gem file.

Then navigate to the newly created folder (qtruby4-2.1.0-x86-mswin32) and open the bin sub-folder (you can optionally open the lib folder in a new Explorer window as well, using CTRL+ENTER. Keep it minimized, will be used in the next step). Select the following .dll files (sort by file type beforehand), basically from Qt3Support4.dll until the end of the folder's files :

Gem-bin

Place the selected files into the clipboard (CTRL+C). Then navigate to your Ruby installation folder's bin sub-folder (within the VM in which I tested this process, the path was C:\ruby-1.8.7-p330-i386-mswin32\bin) and paste. All 37 files are now located in your Ruby's bin folder, awaiting to be found and used by QtRuby. Don't close the Explorer window, minimize it as we'll return here in the next step.

Only one step remains until you can test your installation. Go to the unpacked gem folder (I extracted it to the Desktop for easy access, it was under the C:\Users\Dr1Ku\Desktop\qt\qtruby4-2.1.0-x86-mswin32 path in the VM) and open the lib sub-folder (or maximize the already opened Explorer window). Simply select all files (CTRL+A) and copy them (CTRL+C) into the clipboard as well. Then go to the lib sub-folder of your Ruby installation folder (or up a few folders if the window is still open) and navigate to the following path: [RUBY_DIR]\lib\ruby\site_ruby\1.8 (usual location is C:\ruby-1.8.7-p330-i386-mswin32\lib\ruby\site_ruby\1.8) -- It's a bit of a twisty path indeed. Paste the files. You're good to go!

Open a shell (a 'command prompt', or cmd) and type in irb. Then write require 'qt4'. It should return true, e.g. the QtRuby library was successfully loaded. Now you can write the following test lines to see that QtRuby is working :

a = Qt::Application.new(ARGV)
b = Qt::PushButton.new("abc")
c.resize(100,30)
c.show
a.exec

Now you can also test that Qt 4.6.1 was indeed installed, I chose the FileSystemBrowser Class since it's first been introduced in Qt 4.4 (e.g. a newer version than 4.3.4, the version recommended by the QtRuby installer for Windows mentioned earlier. It works, as you can see below. I also tried including a new Qt 4.7 class , that obviously backfired. An example of the way in which Ruby 1.9 fails to play well with QtRuby 2.1.0 is also included below:

(download)

I tried to run QtRuby on Ruby 1.9.2 as well, yet as pointed out in another article, this is a pipe dream, since QtRuby only works for Ruby 1.8.x. Another caveat is that upgrading the Qt version you're using to compile your Qt/Ruby code does not work, since QtRuby uses the Qt 4.6.1 DLLs as of version 2.1.0. Still, it's better than running Qt 4.3 !

Right, so that would be the conclusion to this small walkthrough, I hope someone will find it useful. Happy hacking !

Don't forget to check this space for an automated way of integrating the sweet Qt Designer into your Qt + Ruby workflow ;), the small script is ready, it must be polished a bit before it hits the limelight. Thanks for reading !

Here's a list of further resources I recommend, related to QtRuby and Qt in general :