Difference between revisions of "Development/Languages/Ruby/Ruby-Qt/KDE Book/First Steps/Hello World"

Jump to: navigation, search
(insert code examples, code works)
(explain hello ruby program)
Line 2: Line 2:
 
series=Ruby|
 
series=Ruby|
 
name=Ruby-Qt/KDE Book|
 
name=Ruby-Qt/KDE Book|
pre=[[Development/Languages/Ruby/Ruby-Qt/KDE Book/Installation/Installation on Linux|Installation on Linux]], [[Development/Languages/Ruby/Ruby-Qt/KDE Book/Installation/Installation on Mac OS|Installation on Mac OS]] or [[Development/Languages/Ruby/Ruby-Qt/KDE Book/Installation/Installation on Windows|Installation on Windows]]
+
pre=[[Development/Languages/Ruby/Ruby-Qt/KDE Book/Installation/Installation on Linux|Installation on Linux]], [[Development/Languages/Ruby/Ruby-Qt/KDE Book/Installation/Installation on Mac OS|Installation on Mac OS]] or [[Development/Languages/Ruby/Ruby-Qt/KDE Book/Installation/Installation on Windows|Installation on Windows]]|
 +
reading=[http://en.wikipedia.org/wiki/Signals_and_slots Signals and Slots-Concept]
 
}}
 
}}
  
Line 10: Line 11:
 
After all you should have a working ruby installation with KDE 4 bindings.
 
After all you should have a working ruby installation with KDE 4 bindings.
  
So lets try the a very minimalistic KDE 4 application to get a first impression.
+
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.
  
= Interactive Hello Ruby =
+
= Hello Ruby =
  
== Implementation Using Qt 4 ==
+
== Interactive Implementation Using Qt 4 ==
 +
 
 +
Open a shell and type in <tt>irb</tt> to start the interactive ruby shell.
 +
Now copy the short code example into the irb session.
  
 
<code ruby>
 
<code ruby>
 
require 'Qt4'
 
require 'Qt4'
 
 
a = Qt::Application.new ARGV
 
a = Qt::Application.new ARGV
w = Qt::PushButton.new( "Hello Ruby" )
+
w = Qt::PushButton.new "Hello Ruby"
 
Qt::Object.connect( w, SIGNAL( :clicked ), a, SLOT( :quit ) )
 
Qt::Object.connect( w, SIGNAL( :clicked ), a, SLOT( :quit ) )
 
w.show
 
w.show
Line 26: Line 29:
 
</code>
 
</code>
  
 +
Before you starting you start programming your Graphical User Interface (GUI), you have to <<tt>require</tt> the libraries, in this case <tt>Qt4</tt>, but <tt>korundum4</tt> 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 <tt>Qt::Application</tt>. <tt>Qt</tt> is the Ruby module (namespace) and <tt>Application</tt> the name of the class in the module. In C++, the native language of Qt, you would write <tt>QApplication</tt>. Remember this as you will need it, if you want to take a look in the Qt documentation.
 +
 +
Another object w of <tt>Qt::PushButton</tt> is created in the third line. <tt>"Hello Ruby"</tt> 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.
  
 
<code ruby>
 
<code ruby>
 +
w = Qt::PushButton.new( "Hello Ruby" )
 +
</code>
 +
 +
The small <tt>w</tt> means widget. All graphical elements in Qt are called widgets.
 +
 +
The fourth line is probably the most complicated one. The ruby class method <tt>connect</tt> of the object <tt>Object</tt> of the module <tt>Qt</tt> 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 [http://en.wikipedia.org/wiki/Signals_and_slots short overview] and other ressources on Wikipedia.
 +
 +
The C++ equivalent you will find in the Qt documentation to <tt>:clicked</tt> would be <tt>"clicked()"</tt>, 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 <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 was closed.
 +
 +
== Say Hello, Ruby ==
 +
 +
 +
<code ruby>
 +
#!/usr/bin/ruby
 +
 
require 'Qt4'
 
require 'Qt4'
  

Revision as of 20:54, 20 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

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

  1. !/usr/bin/ruby

require 'Qt4'

class CustomApplication < Qt::Application

 slots :doSomething
 def doSomething
   puts "Do something else"
   Qt::Application.instance.quit
 end

end

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

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


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal