Languages/Ruby/Ruby-Qt/KDE Book/First Steps/Hello World: Difference between revisions

From KDE TechBase
(add explanations)
(explain second code example)
Line 57: Line 57:
<code ruby>
<code ruby>
#!/usr/bin/ruby
#!/usr/bin/ruby
# file: sayHello.rb
#file: sayHello.rb


require 'Qt4'
require 'Qt4'
Line 66: Line 66:


   def sayHello
   def sayHello
     puts "Hello World"
     msgBox = Qt::MessageBox.new
    msgBox.text = "Hello World"
    msgBox.icon = Qt::MessageBox::Information
    msgBox.standardButtons = Qt::MessageBox::Ok
    val = msgBox.exec()
   
     Qt::Application.instance.quit
     Qt::Application.instance.quit
   end
   end
Line 78: Line 83:
a.exec
a.exec
</code>
</code>
In the last example we connected the button with the existing slot <tt>:close</tt>. Now 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't have any arguments. So we can use the shortcut instead of <tt>"sayHello()"</tt>. After that we define a method with the same name and number of arguments. In the method a message box is prepared which get shown to the user with <tt>exec()</tt>. To define the properties of the message box, the binding specific syntax is used, which allows you to replace <tt>setText( "Hello World" )</tt> with <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> the program gets closed.
The last lines are nearly the same as in the last example. We have to replace <tt>Qt::Application</tt> with our own class <tt>CustomApplication</tt> and change the slot to <tt>:sayHello</tt>.


<code ruby>
<code ruby>

Revision as of 16:21, 21 January 2010

Ruby-Qt/KDE Book
Tutorial Series   Ruby
Previous   Installation on Linux, Installation on Mac OS or Installation on Windows
What's Next   n/a
Further Reading   Signals and Slots-Concept
100%
noframe
noframe

0% completed (estimate)

  

Status Of Writing


After all you should have a working ruby installation with KDE 4 bindings.

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

Hello Ruby

Interactive Implementation Using Qt 4

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

require 'Qt4' a = Qt::Application.new ARGV w = Qt::PushButton.new "Hello Ruby" Qt::Object.connect( w, SIGNAL( :clicked ), a, SLOT( :quit ) ) w.show a.exec

Before you starting you start programming your Graphical User Interface (GUI), you have to <require the libraries, in this case Qt4, but korundum4 would also do the job, because the KDE libs automatically also requires the Qt libs for you.

In the second line we create a new Qt Object a of Qt::Application. Qt is the Ruby module (namespace) and Application the name of the class in the module. In C++, the native language of Qt, you would write QApplication. Remember this as you will need it, if you want to take a look in the Qt documentation.

Another object w of Qt::PushButton is created in the third line. "Hello Ruby" is the first (and only) argument of the Constructor. If you check the Qt documentation, you would find out, that this is the caption of the button. Ruby would also allow you to write it with brackets.

w = Qt::PushButton.new( "Hello Ruby" )

The small w means widget. All graphical elements in Qt are called widgets.

The fourth line is probably the most complicated one. The ruby class method connect of the object Object of the module Qt is used to bind the user action button-clicked with the action application-close. The signals and slots is a special concept of Qt. You can get a short overview and other ressources on Wikipedia.

The C++ equivalent you will find in the Qt documentation to :clicked would be "clicked()", a signal without an argument. In the case of no arguments you can always use a ruby symbol.

The fifth line let the created widget show. Don't forget this line. Maybe you want to think of a starting point of your application.

At last the application gets started by a.exec. Now the GUI takes over and manages the control flow. Any other command after this line will be applied after the GUI was closed.

Say Hello, Ruby

In the next example we want our GUI to do something interactively. Because it is more complex you may want to save it in a file "sayHello.rb".

If you are on a linux-based system, you can add on the top of the file a so called Shebang line. After giving the file execution rights with chmod +x sayHello.rb by using the shell, you can execute your program with a click on the icon in the file browser or in the command line with ./sayRuby.rb.

  1. !/usr/bin/ruby
  2. file: sayHello.rb

require 'Qt4'

class CustomApplication < Qt::Application

 slots :sayHello
 def sayHello
   msgBox = Qt::MessageBox.new
   msgBox.text = "Hello World"
   msgBox.icon = Qt::MessageBox::Information
   msgBox.standardButtons = Qt::MessageBox::Ok
   val = msgBox.exec()
   
   Qt::Application.instance.quit
 end

end

a = CustomApplication.new ARGV w = Qt::PushButton.new( "Say Hello, Ruby" ) Qt::Object.connect( w, SIGNAL( :clicked ), a, SLOT( :sayHello ) ) w.show a.exec

In the last example we connected the button with the existing slot :close. Now we want to create our own slot. We create a new class CustomApplication by inheriting from Qt::Application and add a slot :sayHello. It doesn't have any arguments. So we can use the shortcut instead of "sayHello()". After that we define a method with the same name and number of arguments. In the method a message box is prepared which get shown to the user with exec(). To define the properties of the message box, the binding specific syntax is used, which allows you to replace setText( "Hello World" ) with text = "Hello World". Remember this as you need it to find the class method in the C++ documentation of Qt.

With Qt::Application.instance.quit the program gets closed.

The last lines are nearly the same as in the last example. We have to replace Qt::Application with our own class CustomApplication and change the slot to :sayHello.

require 'Qt4'

a = Qt::Application.new ARGV w = Qt::PushButton.new( "Hello Ruby" ) w.connect( SIGNAL :clicked ) do

   puts "Do something else"
   Qt::Application.instance.quit

end w.show a.exec

require 'Qt4'

a = Qt::Application.new ARGV w = Qt::PushButton.new( "Hello Ruby" ) do

 connect( SIGNAL :clicked ) do
   puts "Do something else"
   Qt::Application.instance.quit
 end

end w.show a.exec