KDevelop TechNotes

Issue 3: RAD with KDevelop using Ruby/Qt/KDE

In the previous issue I wrote about joint GUI design and development with integrated KDevelop Designer using C++ programming language. C++ is a powerful language but for some tasks its power can be superfluous and even harmful. Fortunately other language exist and a group of higher-level languages is available to fit to those development tasks when C++ is not appropriate. Ruby (http://www.ruby-lang.org) is one of those languages and it becomes more and more popular today.

KDevelop has been offering Ruby language support from the times of 3.0.0 release but now some important Ruby-related improvements were made in the development version of KDevelop (CVS HEAD). Notably those are Qt Designer integration for Ruby and Ruby debugger. I will cover the debugger in the latest issues of KDevelop TechNotes. In this issue I am going to concentrate on designer integration.

Ruby distribution offers only facilities to create user interfaces using Tk library. Does this mean that we should forget about modern good-looking and at the same time powerful GUI libraries? Of course, no! Thanks to Richard Dale Qt library bindings for Ruby are available together with complementing tool rubuic (uic analog for ruby) and lots of examples. Those bindings are included into kdebindings package. kdebindings package for KDE versions up from 3.3 should include Qt (qtruby) and KDE (korundum) bindings. Thanks again to Richard Dale those bindings are always in a great shape - stable and useful.

As said above we have powerful Ruby language and can write programs using the best GUI libraries available (Qt and KDE libraries). Guess what do we lack? Yes, right! The IDE to support the development of GUI applications with Ruby and Qt/KDE. And I am proud to say that such an IDE exists now! Current development version of KDevelop supports not only Ruby development but also provides Qt designer integration (in the same way as for C++, see the previous issue of KDevelop TechNotes: http://www.kdevelop.org/doc/technotes/cpprad.html).

In this article I will explain how to write Ruby/Qt and Ruby/KDE applications using KDevelop IDE and mention tools that need to be available for that.

Creating a simple Qt application with a graphical interface

  1. Open KDevelop, choose Project->New Project from the menu and create a Ruby / Qt / QtRuby Hello World.

  2. Select File->New from the menu and create 'mainbase.ui' file from the Widget (.ui) template.

  3. Open 'File Tree' toolview (using button on the left side of KDevelop window).

  4. Double click on 'mainbase.ui' in File tree to open integrated designer. You should now see integrated designer with an empty form.
    Screenshot:
    rubyrad1.png

  5. Change the name of a form to 'MainBase'.

  6. Put a PushButton onto a form and go to 'Signal Handlers' view.

  7. Add a signal handler (by right click menu) to the 'clicked()' signal. After this 'Create or Select Implementation Class for: MainBase' dialog should appear asking you to enter subclass name. Enter, for example, MainImpl as a class name there and press OK.


  8. One file with a form subclass will be created and opened: mainimpl.rb. Cursor will be positioned on the first line of MainImpl::pushButton1_clicked() method definition. You can put your code there (try something like Qt::MessageBox::information(self, "Ruby Application", "Hello World!") for the beginning).
    Screenshot: rubyrad2.png

  9. Open the main application file (which was created by an application wizard) and change its contents to:

    #!/usr/bin/env ruby
    
    require 'Qt'
    require 'mainimpl.rb'
    
    app = Qt::Application.new(ARGV)
    widget = MainImpl.new
    
    app.mainWidget = widget
    widget.show
    app.exec
  10. Save all files (do not forget to explicitly save the mainbase.ui form too).

  11. Build the project. This might look strange for Ruby projects but you need to 'build' them. This is because the makefile contains a rule to run rbuic program on your .ui files and transform .ui to a Ruby code. If build does not end successfully you need to run 'rbuic mainbase.ui' from the command line by yourself.

  12. Run the project. You should get a window with one button.

Note: You can define as many slots as you want. Designer integration will add slot implementations to the subclass and will not overwrite your code in existing slot implementations.

Creating a simple KDE application with a graphical interface

Creation of KDE application is a similar process. You only need to perform following changes:

  1. Optionally use KDE widgets in your forms.

  2. The main application file should look as:

    #!/usr/bin/env ruby
    
    require 'Korundum'
    require 'mainimpl.rb'
    
    about = KDE::AboutData.new("myapp", "MyApplication", "0.1")
    KDE::CmdLineArgs.init(ARGV, about)
    app = KDE::Application.new()
    widget = MainImpl.new
    
    app.setMainWidget(widget)
    widget.show
    app.exec
  1. Makefile should be changed to run 'rbuic -kde' instead of simply 'rbuic'. This notice is also valid if you run rbuic manually.

Notes

KDevelop offers more Ruby application templates like ready-to-use application frameworks and DCOP services. I described only a simple case - an application based on QtRuby Hello World template.

Technical background

Qt Designer integration for Ruby is done in the same manner as for C++. For technical details look at the previous issue of KDevelop TechNotes: http://www.kdevelop.org/doc/technotes/cpprad.html.

Installation

Only development version of KDevelop (CVS HEAD) has features discussed in this article. Usually KDevelop HEAD is kept as stable as possible so it should be safe to upgrade your KDevelop. Next stable version (3.2) of KDevelop with these features will be released according to the KDE release schedule (http://developer.kde.org/development-versions/kde-3.4-release-plan.html).

To work with Ruby you need the Ruby distribution itself. Chances that you already have it installed on your system. If not, check your distribution for packages or get it from http://www.ruby-lang.org.

QtRuby and Korundum bindings are necessary too. kdebindings module from KDE distribution (version up from 3.3) has these. If your distribution does not have precompiled packages it is possible to build bindings from the source. To do this:
get the source from http://download.kde.org/download.php?url=unstable/snapshots/kdebindings.tar.bz2.
unpack and run ./configure
execute make && make install in smoke, qtruby and korundum directories.

Most times it is safe to use kdebindings from CVS.

KDevelop downloads:
Recent development version: http://www.kdevelop.org/index.html?filename=HEAD/download.html.



Discuss this article: http://www.kdevelop.org/phorum5/read.php?2,26470

© 2004 by Alexander Dymo mailto:adymo@kdevelop.org.

Permission to copy, publish and modify this document without restrictions is granted.

Everybody is welcome to share KDevelop knowledge and experience. We accept any materials for the future issues of KDevelop TechNotes.

Reviews:

05.12.2004 - first revision.