Archive:Development/Languages/Ruby (zh TW): Difference between revisions

From KDE TechBase
(Created page with '{{Template:I18n/Language Navigation Bar|Development/Languages/Ruby}} Image:ruby.png 非常完整的綁定包括 KDE API 和 Qt API。The Korundum package includes both a QtR...')
(No difference)

Revision as of 16:29, 28 November 2009


非常完整的綁定包括 KDE API 和 Qt API。The Korundum package includes both a QtRuby Qt-only binding along with the full combined Qt/KDE one. The QtRuby package contains just Qt bindings with no dependencies on KDE.

Korundum/QtRuby - Ruby-KDE/Qt bindings

The book Rapid GUI Development with QtRuby (for Qt version 3.x) is available.

Being Smoke-based bindings means that they offer full access to most KDE 4.x and Qt 4.x classes.


Hello world 範例:

  1. !/usr/bin/ruby -w

require 'Qt4' a = hello ="Hello World!") hello.resize(100, 30) a.exec

更'Rubyish'方式的 Hello Qt 範例:

require 'Qt4' do do
       self.window_title = 'Hello QtRuby v1.0'
       resize(200, 100)
       button ='Quit') do
           connect(SIGNAL :clicked) { Qt::Application.instance.quit }
       label ='Hello Qt in the Ruby way!')
       self.layout = do
           add_widget(label, 0, Qt::AlignCenter)
           add_widget(button, 0, Qt::AlignRight)


目前 API 範圍概覽


You can call all Qt public and protected methods, and all friend methods such as bitBlt() etc


All virtual methods can be overridden, not just event handlers


'foobar = 5' is a synonym for 'setFooBar(5)'

Use either CamelCase or lowercase with underscore naming

Any underscores in method names are removed, and the following character is capitalised. For example, you can use either of these two forms to call the same method:


Operator overloading

The full range of Qt operator methods is available, for example:

      p1 =,5)   => (5, 5)
      p2 =,20) => (20, 20)
      p1 + p2                   => (25, 25)

Declare signals and slots

Signals and slots are declared as list of strings like this:

      slots 'setColor(QColor)', 'slotLoad(const QString&)'..
      signals 'clicked()'..

For slots and signals without arguments you can use Ruby symbols:

      slots :slotLoad
      signals :clicked

Currently C++ type signatures must be used, a future version of QtRuby will allow ruby type signatures instead.

Connect slots and signals like this:

      Qt::Object.connect( @colormenu, SIGNAL( "activated(int)" ),
                        self, SLOT( "slotColorMenu(int)" ) )

Or you can connect signal to a block:

      quit_button.connect(SIGNAL :clicked) { $qApp.quit }

And emit signals like this:

      emit colorChanged( black )


You can call constructors in the conventional style:

      quit ="Quit", self, "quit")

Or you can pass a block if you prefer:

      w = { setCaption("foobar") }

The block will be called in the context of the newly created instance.

Ordinary arguments can be provided as well as a block at the end:

      w = { setCaption("foobar") }

They are run in the context of the new instance.

And there's more! You can also pass an arg to the block, and it will be run in the context of the arg:

      w = { |theWidget| theWidget.setCaption "foobar" }

Garbage Collection

When a ruby instance is garbage collected, the underlying C++ instance will only be deleted if it isn't 'owned' by a parent object. Normally this will 'just work', but there are occasions when you need to delete the C++ ahead of garbage collection, and whether or not it has a parent. Use the dispose(), isDisposed() and disposed? methods like this:

      if item2.disposed?
      	puts "item2 is disposed"

C++ 'int*' and 'int&' argument types

Ruby passes numeric values by value, and so they can't be changed when passed to a method. The Qt::Integer class provides a mutable numeric type which does get updated when passed as an argument. For example, this C++ method 'findByFileContent()':

      # static Ptr findByFileContent( const QString &fileName, 
      #                               int *accuracy=0 );

      acc =
      fc = KDE::MimeType.findByFileContent("mimetype.rb", acc)

It supports the arithmetic operators, and so expressions such as 'acc + 3' will work.

C++ 'bool*' and 'bool&' argument types

There is a similar problem for bool arg types, and the mutable Qt::Boolean class can be used like this:

      # QFont getFont(bool * ok, const QFont&initial, 
      #               QWidget* parent = 0, const char *name = 0);		
      ok =
      font = Qt::FontDialog.getFont(ok, 
                "Helvetica [Cronyx]", 10), 
      if !ok.nil? 
      	# font is set to the font the user selected
      	# the user canceled the dialog

Use 'nil?' to test the value returned in the Boolean

C++ (const )(unsigned )char* argument types

In some cases Qt/KDE object "takes ownership" over Ruby String passed as char* argument type. Programmer needs to make sure that Ruby String is not being garbage collected or changed for the time it's being used by Qt/KDE object. It is also quite possible that Qt/KDE object will change and eventually free it(memory used internally by Ruby String to store its data). Be very careful when you call this kind of methods and make sure that there is no overloaded version witch accepts QString or QByteArray first!

C++ unsigned char* functions

Very few functions (as QImage::bits()) return a uchar* to directly manipulate data. These functions are not supported in Ruby and will throw an ArgumentError. More information on the mail list.


If a method call can't be matched in the Smoke library giving a 'method_missing' error, first check that you are passing correct class instance that is properly initialized (with super method called in constructors of custom Qt classes descendants). You can also turn on debugging to trace the matching process:

      a =
      Qt.debug_level = Qt::DebugLevel::High
      a.loadLibrary("foo")  # Non existent method

Will give the following output:

      classname    == QApplication
      :: method == loadLibrary$
      -> methodIds == []
      candidate list:
      Possible prototypes:
          static QWidget* QApplication::widgetAt(int, int, bool)

Here, the list of candidate methods 'methodIds' is empty

Another debugging mechanism allows various trace 'channels' to be switched on.

You can trace virtual method callbacks:


Or trace QtRuby garbage collection:


String i18n

QtRuby supports $KCODE values of 'u', 'e' and 's' or the corresponding '-K' options from the command line. Qt Designer .ui files have UTF-8 strings so if you use any 8 bit UTF-8 characters, you will need to set $KCODE='u' or use the -Ku command line option.

Other capabilities and offerings

Qt Designer

A 'rbuic4' tool is included in qtruby/tools/rbuic to compile .ui files into ruby code. As described above, Qt Designer uses UTF-8. In addition to the options in the original uic C++ utility an '-x' flag has been added. This will generate a top level stub in the code:

      $ rbuic mainform.ui -x -o mainform.rb

Will add this to the end of the generated code:

      if $0 == __FILE__
          a =
          w =


Then you can test the example code straight away:

      $ ruby mainform.rb

Use the '-kde' option to require the 'korundum4' extension rather than the 'Qt4' one. If the '-x' option is used in conjunction, it generates a KDE top level. For example:

      $ rbuic4 -x -kde knotifywidgetbase.ui -o knotifywidgetbase.rb

Will generate this top level code:

      if $0 == __FILE__
      	about ="knotifywidgetbase", 
		                           "KNotifyWidgetBase", "0.1")
      	KDE::CmdLineArgs.init(ARGV, about)
        a =
    	w =

Loading .ui files at runtime with Qt::UILoader

This section needs improvements: Please help us to

cleanup confusing sections and fix sections which contain a todo

Remove example that does not work

You can load a Qt Designer .ui file at runtime with the 'quiloader' extension, for example:

      require 'Qt4'
      require 'quiloader'

      a =
      if ARGV.length == 0

      if ARGV.length == 2
        QUI::WidgetFactory.loadImages ARGV[0]
        w = QUI::WidgetFactory.create ARGV[1]
        if w.nil?
        a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))

With new version API changed a little.

      require 'Qt4'
      require 'qtuitools'

      a =
      if ARGV.length == 0

      if ARGV.length == 1
        file =[1])

        loader =
        window = loader.load(file, nil)
        if (window.nil?)
          print "Error. Window is nil.\n"
        a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))

API reference

Use the bin/rbqtapi tool to discover which methods are available in the QtRuby api. This command:

	  $ rbqtapi Qt::TextEdit

Will list all the methods in the Qt::TextEdit class

	  $ rbqtapi -rsetCaption 

Lists all methods whose names contain the string 'setCaption'

Example programs

The best way to start programming QtRuby is to look at some existing code and start messing with it.. The are various samples under qtrubyexamples and korundum/examples.

KDE Specific Infomation

Instead of require 'Qt4', use require 'korundum4' for KDE programs.

The KDE K* classes such as KApplication are renamed as KDE::Application. The other KDE classes are in the KParts::, KIO:: or DOM:: namespaces, with the same names as their C++ counterparts.

Use the 'rbkdeapi' script to introspect the Korundum api from the command line. For example:

      $ rbkdeapi KDE::Action

Will list all the methods in the KDE::Action class. There are currently (as at KDE 3.3 beta 2) 977 classes/30841 methods in the Smoke library runtime, so the coverage of the Qt/KDE api is pretty complete.

Build dependencies

  • ruby 1.8 or greater (svn trunk works with 1.9.1)
  • cmake 2.6 or greater
  • Qt 4.0 or greater
  • KDE 4.1 or greater (for korundum)


There is a ruby translation of Qt Tutorial #1, and the corresponding ruby code is in qtruby/rubylib/tutorial/t1 to t14.

And a Qt4 version of the same tutorial translated to Ruby by Darshan Ishaya Qt4 Tutorial #1

Qt Tutorial #2, a Charting Application with ruby code in qtruby/rubylib/examples/qt-examples/chart.

The Qt Designer Color Tool Tutorial, with ruby code in qtruby/rubylib/designer/examples/colortool.

Paul Lutus has written a tutorial on how to get started with Ruby GUI programming with Qt

For KDE, there is a ruby translation of this KDE 3.0 tutorial originally written for C++ by Antonio Larrosa Jiménez. The sources are in korundum/rubylib/tutorials/p1 to p9.

The book Rapid GUI Development with QtRuby is now available.


You can obtain recent SVN snapshots on the Rubyforge QtRuby/Korundum site.

More help

There are two IRC channels (#qtruby and #kde-ruby) in FreeNode. If you prefer e-mail, you can use the kde-bindings mailing-list (low traffic) or ask in the ruby-talk mailing list (you may use the Ruby Forum gateway to post in ruby-talk from web).

More information

A series of articles on ruby QT (inspired by the work done for the dradis project):