So let's try out a very minimalistic Qt 4 application to get a first impression. We don't start with KDE because that would require some extra lines which are not necessary when using Qt.

Hello Ruby

Interactive Implementation Using Qt 4

Open a shell and type irb to start the interactive ruby shell.
Now copy this short code example into the irb session.

require'Qt4'a=Qt::Application.newARGVw=Qt::PushButton.new"Hello Ruby"Qt::Object.connect(w,SIGNAL(:clicked),a,SLOT(:quit))w.showa.exec</code>Before you start programming your Graphical User Interface (GUI) you have to <tt>require</tt>thelibraries,inthiscase<tt>Qt4</tt>, but <tt>korundum4</tt>wouldalsodothejobbecausetheKDElibsautomaticallyrequirestheQtlibsforyou.InthesecondlinewecreateanewQtObject<tt>a</tt> of <tt>Qt::Application</tt>.<tt>Qt</tt> is the Ruby module (namespace) and <tt>Application</tt>thenameoftheclassinthemodule.InC++,thenativelanguageofQt,youwouldwrite<tt>QApplication</tt>. Remember this as you will need it when you want to take a look at the Qt documentation, which assumes you use C++.Another object <tt>w</tt>of<tt>Qt::PushButton</tt> is created in the third line. <tt>"Hello Ruby"</tt>isthefirst(andonly)argumentoftheConstructor.IfyouchecktheQtdocumentation,youwouldfindout,thatthisisthecaptionofthebutton.Rubywouldalsoallowyoutowriteitwithbrackets.<syntaxhighlightlang="ruby">w=Qt::PushButton.new("Hello Ruby")</code>The small <tt>w</tt>meanswidget.AllgraphicalelementsinQtarecalledwidgets.Thefourthlineisprobablythemostcomplicatedone.Therubyclassmethod<tt>connect</tt> of the object <tt>Object</tt>ofthemodule<tt>Qt</tt> is used to bind the user action button-clicked with the action application-close. Signals and slots is a special concept of Qt. You can get a [http://en.wikipedia.org/wiki/Signals_and_slotsshortoverview],andfindotherresources,onWikipedia.TheC++equivalentyouwillfindintheQtdocumentationto<tt>:clicked</tt> would be <tt>"clicked()"</tt>,asignalwithoutanargument.Inthecaseofnoargumentsyoucanalwaysusearubysymbol.Thefifthlineisrequiredtomakethecreatedwidgetactuallyappearonthescreen.Don't forget this line. Maybe you want to think of this as the starting point of your application.And finally the application gets started by <tt>a.exec</tt>. Now the GUI takes over and manages the control flow. Any other command after this line will be applied after the GUI has been closed by the user of the application.== Say Hello, Ruby ==In the next example we want our GUI to do something interactive. Because it is more complex you may want to save it in a file "sayHello.rb".Clicking the file opens it in your text editor, but on Linux we can also make it start the application. Look at the top of the improved Ruby application below. That is called a [http://en.wikipedia.org/wiki/Shebang_%28Unix%29 Shebang] line. After saving the file we need to tell Linux that the file is allowed to be executed by default with the shell command:<syntaxhighlight lang="bash">chmod +x sayHello.rb</code>After giving the file execution rights you can execute your program on the command line with <tt>./sayHello.rb</tt> or with a click on the icon in the file browser (Dolphin, Konqueror).<syntaxhighlight lang="ruby">#!/usr/bin/ruby#file: sayHello.rbrequire 'Qt4'class CustomApplication < Qt::Application slots :sayHello def sayHello msgBox = Qt::MessageBox.new msgBox.text = "Hello World" msgBox.icon = Qt::MessageBox::Information val = msgBox.exec() Qt::Application.instance.quit endenda = CustomApplication.new ARGVw = Qt::PushButton.new( "Say Hello, Ruby" )Qt::Object.connect( w, SIGNAL( :clicked ), a, SLOT( :sayHello ) )w.showa.exec</code>In the previous example we connected the button with the existing slot <tt>:close</tt>. Now, in the example above, we want to create our own slot. We create a new class <tt>CustomApplication</tt> by inheriting from <tt>Qt::Application</tt> and add a slot <tt>:sayHello</tt>. It doesn'thaveanyarguments.Sowecanusetheshortcut<tt>":sayHello"</tt> instead of <tt>":sayHello()"</tt>.Afterthatwedefineamethodwiththesamenameandanumberofarguments.Inthemethodamessageboxispreparedwhichgetshowntotheuserwith<tt>exec()</tt>. To define the properties of the message box, the binding specific syntax is used, which allows you to replace the C++ styled code<tt>setText( "Hello World" )</tt>withtheRubystyledcode<tt>text="Hello World"</tt>Remember this as you need it to find the class method in the C++ documentation of Qt.With <tt>Qt::Application.instance.quit</tt>theprogramgetsclosed.Thelastlinesarenearlythesameasinthelastexample.Wehavetoreplace<tt>Qt::Application</tt> with our own class <tt>CustomApplication</tt>andchangetherequesttoquitintheslotto<tt>:sayHello</tt> in the connection command.== Say Hello, Ruby - the short version ==<syntaxhighlight lang="ruby">#!/usr/bin/ruby#file: sayHello.rbrequire'Qt4'a=Qt::Application.newARGVw=Qt::PushButton.new("Say Hello, Ruby")w.connect(SIGNAL(:clicked))domsgBox=Qt::MessageBox.newmsgBox.text="Hello World"msgBox.icon=Qt::MessageBox::Informationval=msgBox.exec()Qt::Application.instance.quitendw.showa.exec</code>The bindings offer special features, allowing you to define the actions for a given signal without having to create a slot. Just use the <tt>connect</tt>methodoftheQtobjectwhichownsthesignalyouwanttouse.The<tt>connect</tt> expects you to give only one argument, the signal, followed by a code block (<tt>do ... end</tt>),whichgetsprocessedincasethesignalistriggered.Thiswayofprogrammingallowsaverycompactstyle.Inthiscaseyoudon't need to create a <tt>CustomApplication</tt>.== Say Hello, Ruby - an even shorter version ==<syntaxhighlight lang="ruby">require 'Qt4'a = Qt::Application.new ARGVw = Qt::PushButton.new( "Hello Ruby" ) do self.connect( SIGNAL :clicked ) do msgBox = Qt::MessageBox.new msgBox.text = "Hello World" msgBox.icon = Qt::MessageBox::Information val = msgBox.exec() Qt::Application.instance.quit endendw.showa.exec</code>If you create a new object you can add a code block with some statements that gets executed in the namespace of the new object. You can also use <tt>do</tt> to inline the code belonging to <tt>msgBox</tt>, too:<syntaxhighlight lang="ruby">require 'Qt4'a=Qt::Application.newARGVw=Qt::PushButton.new("Hello Ruby")doself.connect(SIGNAL:clicked)domsgBox=Qt::MessageBox.newdoself.text="Hello World"self.icon=Qt::MessageBox::Informationendval=msgBox.exec()Qt::Application.instance.quitendendw.showa.exec</code>