Difference between revisions of "Development/Tutorials/Qt4 Ruby Tutorial/Chapter 06"

Jump to: navigation, search
m (Text replace - "<code ruby>" to "<syntaxhighlight lang="ruby">")
m (Text replace - "</code>" to "</syntaxhighlight>")
Line 68: Line 68:
  
 
app.exec()
 
app.exec()
</code>
+
</syntaxhighlight>
  
 
=== Line by Line Walkthrough ===
 
=== Line by Line Walkthrough ===
 
<syntaxhighlight lang="ruby">
 
<syntaxhighlight lang="ruby">
 
class LCDRange < Qt::Widget
 
class LCDRange < Qt::Widget
</code>
+
</syntaxhighlight>
  
 
The '''<tt>LCDRange</tt>''' widget is a widget without any API. It just has a constructor. This sort of widget is not very useful, so we'll add some API later.  
 
The '''<tt>LCDRange</tt>''' widget is a widget without any API. It just has a constructor. This sort of widget is not very useful, so we'll add some API later.  
Line 92: Line 92:
 
   setLayout(layout)
 
   setLayout(layout)
 
end
 
end
</code>
+
</syntaxhighlight>
  
 
This is lifted straight from the '''<tt>MyWidget</tt>''' constructor in Chapter 5. The only differences are that the <strong>Quit</strong> button is left out and the class is renamed.  
 
This is lifted straight from the '''<tt>MyWidget</tt>''' constructor in Chapter 5. The only differences are that the <strong>Quit</strong> button is left out and the class is renamed.  
Line 98: Line 98:
 
<syntaxhighlight lang="ruby">
 
<syntaxhighlight lang="ruby">
 
class MyWidget < Qt::Widget
 
class MyWidget < Qt::Widget
</code>
+
</syntaxhighlight>
  
 
'''<tt>MyWidget</tt>''', too, contains no API except a constructor.  
 
'''<tt>MyWidget</tt>''', too, contains no API except a constructor.  
Line 108: Line 108:
 
   quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
 
   quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
 
   connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
 
   connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
</code>
+
</syntaxhighlight>
  
 
The push button that used to be in what is now '''<tt>LCDRange</tt>''' has been separated so that we can have one <strong>Quit</strong> button and many '''<tt>LCDRange</tt>''' objects.  
 
The push button that used to be in what is now '''<tt>LCDRange</tt>''' has been separated so that we can have one <strong>Quit</strong> button and many '''<tt>LCDRange</tt>''' objects.  
Line 114: Line 114:
 
<syntaxhighlight lang="ruby">
 
<syntaxhighlight lang="ruby">
 
     grid = Qt::GridLayout.new()
 
     grid = Qt::GridLayout.new()
</code>
+
</syntaxhighlight>
  
 
We create a [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] with a [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] that will contain three columns. The [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] automatically arranges its widgets in rows and columns; you can specify the row and column numbers when adding widgets to the layout, and [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] will fit them into the grid.  
 
We create a [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] with a [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] that will contain three columns. The [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] automatically arranges its widgets in rows and columns; you can specify the row and column numbers when adding widgets to the layout, and [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] will fit them into the grid.  
Line 124: Line 124:
 
   end
 
   end
 
end
 
end
</code>
+
</syntaxhighlight>
  
 
We create nine '''<tt>LCDRange</tt>''' widgets, all of which are children of the grid object, and we arrange them in three rows and three columns.  
 
We create nine '''<tt>LCDRange</tt>''' widgets, all of which are children of the grid object, and we arrange them in three rows and three columns.  

Revision as of 21:55, 29 June 2011


Contents

Development/Tutorials/Qt4 Ruby Tutorial/Chapter 06


Building Blocks Galore!
Tutorial Series   Qt4 Ruby Tutorial
Previous   Tutorial 5 - Building Blocks
What's Next   Tutorial 7 - One Thing Leads to Another
Further Reading   n/a

Building Blocks Galore!

Qt4 Ruby Tutorial Screenshot 6.png

Files:

Overview

This example shows how to encapsulate two widgets into a new component and how easy it is to use many widgets. For the first time, we use a custom widget as a child widget.

require 'Qt4'
 
class LCDRange < Qt::Widget
  def initialize(parent = nil)
    super()
    lcd = Qt::LCDNumber.new(2)
 
    slider = Qt::Slider.new(Qt::Horizontal)
    slider.setRange(0, 99)
    slider.setValue(0)
 
    connect(slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))
 
    layout = Qt::VBoxLayout.new()
    layout.addWidget(lcd)
    layout.addWidget(slider)
    setLayout(layout)
  end
end
 
class MyWidget < Qt::Widget
  def initialize(parent = nil)
    super()
    quit = Qt::PushButton.new(tr('Quit'))
    quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
    connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
 
    grid = Qt::GridLayout.new()
 
    for row in 0..2
      for column in 0..2
        grid.addWidget(LCDRange.new(), row, column)
      end
    end
 
    layout = Qt::VBoxLayout.new()
    layout.addWidget(quit)
    layout.addLayout(grid)
    setLayout(layout)
  end
end    
 
app = Qt::Application.new(ARGV)
 
widget = MyWidget.new()
widget.show()
 
app.exec()

Line by Line Walkthrough

class LCDRange < Qt::Widget

The LCDRange widget is a widget without any API. It just has a constructor. This sort of widget is not very useful, so we'll add some API later.

def initialize(parent = nil)
  super()
  lcd = Qt::LCDNumber.new(2)
  slider = Qt::Slider.new(Qt::Horizontal)
  slider.setRange(0, 99)
  slider.setValue(0)
 
  connect(slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))
 
  layout = Qt::VBoxLayout.new()
  layout.addWidget(lcd)
  layout.addWidget(slider)
  setLayout(layout)
end

This is lifted straight from the MyWidget constructor in Chapter 5. The only differences are that the Quit button is left out and the class is renamed.

class MyWidget < Qt::Widget

MyWidget, too, contains no API except a constructor.

def initialize(parent = nil)
  super()
  quit = Qt::PushButton.new(tr('Quit'))
  quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
  connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))

The push button that used to be in what is now LCDRange has been separated so that we can have one Quit button and many LCDRange objects.

    grid = Qt::GridLayout.new()

We create a Qt::Widget with a Qt::GridLayout that will contain three columns. The Qt::GridLayout automatically arranges its widgets in rows and columns; you can specify the row and column numbers when adding widgets to the layout, and Qt::GridLayout will fit them into the grid.

for row in 0..2
  for column in 0..2
    grid.addWidget(LCDRange.new(), row, column)
  end
end

We create nine LCDRange widgets, all of which are children of the grid object, and we arrange them in three rows and three columns.

Running the Application

This program shows how easy it is to use many widgets at a time. Each one behaves like the slider and LCD number in the previous chapter. Again, the difference lies in the implementation.

Exercises

Initialize each slider with a different/random value on startup.


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