Development/Tutorials/Qt4 Ruby Tutorial/Chapter 05/fi: Difference between revisions

From KDE TechBase
(Created page with "Milloin tahansa liukukytkimen arvo muuttuu, se lähettää uuden arvon [http://doc.qt.nokia.com/latest/qabstractslider.html#valueChanged QAbstractSlider::valueChanged()] -signaal...")
(Created page with "'''<tt>MyWidget</tt>''' nyt käyttää komponenttia [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] hallinnoimaan sen tytärkäyttöliittymäkomponentin geometri...")
Line 85: Line 85:
</syntaxhighlight>
</syntaxhighlight>


'''<tt>MyWidget</tt>''' now uses a [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] to manage the geometry of its child widgets. For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets to the layout using [http://doc.qt.nokia.com/latest/qboxlayout.html#addWidget Qt::BoxLayout::addWidget()].  
'''<tt>MyWidget</tt>''' nyt käyttää komponenttia [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] hallinnoimaan sen tytärkäyttöliittymäkomponentin geometriaa. Siitä syystä meidän ei tarvitse määritellä näytön koordinaatteja jokaiselle käyttöliitymäkomponentille, kuten teimme kappaleessa 4. Sen lisäksi, asettelun käyttäminen varmistaa, että tytärkomponentin koko muuttuu, kun käyttöliittymäkomponentin kokoa muutetaan. Lisäämme sitten '''<tt>quit</tt>'''-, '''<tt>lcd</tt>'''-, ja '''<tt>slider</tt>'''-käyttöliittymäkomponentit asetteluun käyttäen komponenttimetodia [http://doc.qt.nokia.com/latest/qboxlayout.html#addWidget Qt::BoxLayout::addWidget()].  


The [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] function installs the layout on '''<tt>MyWidget</tt>'''. This makes the layout a child widget of '''<tt>MyWidget</tt>''' so we don't have to worry about deleting it; it will be deleted together with '''<tt>MyWidget</tt>'''. Also, the call to [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] automatically reparents the widgets in the layout so that they are children of '''<tt>MyWidget</tt>'''. Because of this, we didn't need to specify '''<tt>self</tt>''' as the parent for the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets.  
The [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] function installs the layout on '''<tt>MyWidget</tt>'''. This makes the layout a child widget of '''<tt>MyWidget</tt>''' so we don't have to worry about deleting it; it will be deleted together with '''<tt>MyWidget</tt>'''. Also, the call to [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] automatically reparents the widgets in the layout so that they are children of '''<tt>MyWidget</tt>'''. Because of this, we didn't need to specify '''<tt>self</tt>''' as the parent for the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets.  

Revision as of 09:47, 28 August 2011

Other languages:


Development/Tutorials/Qt4 Ruby Tutorial/Chapter 05


Rakentamislohkot
Tutorial Series   Qt4 Ruby -oppikurssi
Previous   Oppikurssi 4 - Olkoon siinä käyttöliittymäkomponentti
What's Next   Oppikurssi 6 - Rakentamislohkot Galore!
Further Reading   n/a

Rakentamislohkot

Tiedostot:

Yleiskuva

Tämä esimerkki näyttää kuinka useita käyttöliittymäkomponentteja luodaan ja yhdistetään yhteen käyttäen signaaleja ja aikavälejä, ja kuinka koon muuttamisia käsitellään.

require 'Qt4'

class MyWidget < Qt::Widget
  def initialize()
    super()
    quit = Qt::PushButton.new('Poistu')
    quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
    
    lcd = Qt::LCDNumber.new(2)

    slider = Qt::Slider.new(Qt::Horizontal)
    slider.setRange(0, 99)
    slider.setValue(0)

    connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
    connect(slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))

    layout = Qt::VBoxLayout.new()
    layout.addWidget(quit)
    layout.addWidget(lcd)
    layout.addWidget(slider)
    setLayout(layout)
  end
end

app = Qt::Application.new(ARGV)

widget = MyWidget.new()

widget.show()
app.exec()

Läpikäynti rivi riviltä

    lcd = Qt::LCDNumber.new(2)

lcd on Qt::LCDNumber, käyttöliitymäkomponentti, joka näyttää numeroita nestekidenäytön tyyppisesti. Tämä ilmentymä on asetettu näyttämään kahta numeroa.

slider = Qt::Slider.new(Qt::Horizontal)
slider.setRange(0, 99)
slider.setValue(0)

Käyttäjä voi käyttää Qt::Slider -käyttöliittymäkomponenttia säätämään kokon aislukuarvon lukualueella. Tässä luomme vaakasuoran komponentin, asetetaan sen minimiarvoksi 0, sen maksimiarvoksi 99, ja sen alkuarvoksi 0.

    connect(slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))

Tässä käytämme signaalit ja aikavälit -mekanismia liukukytkimen kytkemiseksi QAbstractSlider::valueChanged() -signaaliin nestekidenumeronäytön display() -aikavälissä.

Milloin tahansa liukukytkimen arvo muuttuu, se lähettää uuden arvon QAbstractSlider::valueChanged() -signaalilla. Koska tuo signaali on yhdistetty nestekidenäytön numeroiden QLCDNumber::display() -aikaväliin, aikaväliä kutsutaan, kun signaalia lähetetään. Kumpikaan komponenteista ei tiedä toisesta. Tämä on olennaista komponenttiohjelmoinnissa.

layout = Qt::VBoxLayout.new()
layout.addWidget(quit)
layout.addWidget(lcd)
layout.addWidget(slider)
setLayout(layout)

MyWidget nyt käyttää komponenttia Qt::VBoxLayout hallinnoimaan sen tytärkäyttöliittymäkomponentin geometriaa. Siitä syystä meidän ei tarvitse määritellä näytön koordinaatteja jokaiselle käyttöliitymäkomponentille, kuten teimme kappaleessa 4. Sen lisäksi, asettelun käyttäminen varmistaa, että tytärkomponentin koko muuttuu, kun käyttöliittymäkomponentin kokoa muutetaan. Lisäämme sitten quit-, lcd-, ja slider-käyttöliittymäkomponentit asetteluun käyttäen komponenttimetodia Qt::BoxLayout::addWidget().

The Qt::Widget::setLayout() function installs the layout on MyWidget. This makes the layout a child widget of MyWidget so we don't have to worry about deleting it; it will be deleted together with MyWidget. Also, the call to Qt::Widget::setLayout() automatically reparents the widgets in the layout so that they are children of MyWidget. Because of this, we didn't need to specify self as the parent for the quit, lcd, and slider widgets.

In Qt, widgets are either children of other widgets (e.g. self), or they have no parent. A widget can be added to a layout, in which case the layout becomes responsible for managing the geometry of that widget, but the layout can never act as a parent itself. Indeed, Qt::Widget's constructor takes a Qt::Widget pointer for the parent, and Qt::Layout doesn't inherit from Qt::Widget.

Running the Application

The LCD number reflects everything you do to the slider, and the widget handles resizing well. Notice that the LCD number widget changes in size when the window is resized (because it can), but the others stay about the same (because otherwise they would look strange).

Exercises

Try changing the LCD number to add more digits or to change mode (Qt::LCDNumber::setMode()). You can even add four push buttons to set the number base.

You can also change the slider's range.

Perhaps it would have been better to use Qt::SpinBox than a slider?

Try to make the application quit when the LCD number overflows.