Tales of a GeekTrotter Binary Logbook

7Jan/110

QtRuby: Rapid programming of powerful graphical applications

Please allow me to introduce you QtRuby, a powerful multi-platform programming language that will help you to develop robust and awesome graphical applications quite easily and funnily. (that's said!)

Install Qt and Ruby

The first point is that it is easy to install both on Windows and Linux.

On Linux

Debian/Ubuntu: sudo apt-get install ruby ruby-dev rubygems libqt4-ruby libqt4-dev qt4-qmake cmake
Red-Hat/Fedora: sudo yum install ruby ruby-devel rubygems libqt4-ruby libqt4-devel qt4-qmake cmake

Then install the Qt Bindings for Ruby: sudo gem install qtbindings

On Windows

    Install the last Qt 4 from Nokia Qt website (choose the LGPL version).
    - If you plan to develop in Qt/C++ you should use the SDK for Windows (that comes with Qt Creator, a very nice Qt/C++ IDE)
    - If not, you can install just the framework (VS 2008 version if you have Visual Studio 2008 installed, minGW if not)
    I recommend the full Qt SDK.
    Then install Ruby easily with the RubyInstaller.
    After that, start a command prompt with Ruby (it should show up when you start searching "ruby" in the start menu).
    Finally install the Qt Bindings with the following command: gem install qtbindings

That's it, we are ready to use Qt with Ruby!

Our first QtRuby program

Open your favourite text editor, it's time to enter some code!
We are going to write some Ruby code that uses the Qt framework.
So we will have the simplicity and beauty of Ruby plus the power of Qt!
To start doing so, we need to import the Qt library in Ruby.

This is done by starting our script by require 'Qt4' .
And we need to start a Qt application by registering the app: app = Qt::Application.new(ARGV).
Then, let say we want to create a  simple button with "Hello World" written inside: hello = Qt::PushButton.new('Hello World')
and resize it by 200x60: hello.resize(200, 60)

Good! Now we want that when we click the button, we show a message and quit.
This will be done by using the Qt signal "clicked" of the button, that is raised when the button is clicked: hello.connect(SIGNAL :clicked)
When the button is clicked, we want to  show a message: Qt::MessageBox.new(Qt::MessageBox::Information, "dallagnese.fr", "Ruby Rocks!").exec
And to quit: app.quit
The code is almost done.
Now we want our previously defined object to be shown, isn't it? --> hello.show
And to start the application: app.exec

That's it! The previous code is summarized in the following file:

require 'Qt4'

app = Qt::Application.new(ARGV)

hello = Qt::PushButton.new('Hello World')
hello.resize(200, 60)
hello.connect(SIGNAL :clicked) {
  Qt::MessageBox.new(Qt::MessageBox::Information, "dallagnese.fr", "Ruby Rocks!").exec
  app.quit
}
hello.show

app.exec

To start the program, you just need to execute the code with Ruby.

On Windows, a double click on the file (hello.rb) should make it.

If not, start the ruby command prompt again, navigate to the hello.rb directory (cd C:\Users\You\...) and run "ruby hello.rb"

Ruby Hello World Example Windows 7

On Linux, navigate to the directory containing hello.rb and execute "ruby hello.rb".

Ruby Hello World Example Linux

More information

I'm sure you want to know more about it!

The first place to look around is the KDE TechBase: http://techbase.kde.org/Development/Languages/Ruby

You will have all the required information to start developing QtRuby applications by yourself on that page 😉

Enjoy!