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

From KDE TechBase
(Created page with "Argumentti on sen emokäyttöliittymäkomponentti. Ylimmän tason ikkunan luomiseksi määrittele emokomponentiksi '''<tt>nil</tt>'''. Kuten voit nähdä, tämän käyttöliittym...")
(Created page with "Kuten useimmat käyttöliittymäkomponentit, se vain välittää '''<tt>parent</tt>'''-parametrin [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget]-konstruktorille. (Jos o...")
Line 60: Line 60:
Argumentti on sen emokäyttöliittymäkomponentti. Ylimmän tason ikkunan luomiseksi määrittele emokomponentiksi '''<tt>nil</tt>'''. Kuten voit nähdä, tämän käyttöliittymäkomponentin oletukset osoittavat sen olevan ylimmän tason ikkuna.  
Argumentti on sen emokäyttöliittymäkomponentti. Ylimmän tason ikkunan luomiseksi määrittele emokomponentiksi '''<tt>nil</tt>'''. Kuten voit nähdä, tämän käyttöliittymäkomponentin oletukset osoittavat sen olevan ylimmän tason ikkuna.  


Like most widgets, it just passes on the '''<tt>parent</tt>''' parameter to the [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] constructor. (If you're new to Ruby, calling '''<tt>super</tt>''' without any parameters will automatically pass on any parameters a method received to the superclass's method of the same name. ''Update'': Apparently, it's also important to leave off the parentheses calling '''<tt>super()</tt>''' will call the superclass's method of the same name with an empty parameter list.)
Kuten useimmat käyttöliittymäkomponentit, se vain välittää '''<tt>parent</tt>'''-parametrin [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget]-konstruktorille. (Jos olet uusi Ruby-kielessä, niin kutsumalla '''<tt>super</tt>''' ilman mitään parametreja välitetään automaattisesti kaikki metodin vastaanottamat parametrit superluokan samannimiselle metodille. ''Update'': Ilmeisesti on myös tärkeää jättää pois sulkeet kutsumalla '''<tt>super()</tt>''' kutsutaan superluokan samanniminen metodi tyhjällä parametriluettelolla.)


<syntaxhighlight lang="ruby">
<syntaxhighlight lang="ruby">

Revision as of 11:53, 27 August 2011

Other languages:


Development/Tutorials/Qt4 Ruby Tutorial/Chapter 04


Olkoon siellä käyttöliittymäkomponentteja
Tutorial Series   Qt4 Ruby -oppikurssi
Previous   Oppikurssi 3 - Perhearvot
What's Next   Oppikurssi 5 - Lohkojen rakentaminen
Further Reading   n/a

Olkoon siellä käyttöliittymäkomponentteja

Tiedostot:

Yleiskuva

Tämä esimerkki näyttää kuinka luot oman käyttöliittymäkomponentin, ja kuvailee kuinka ohjaat käyttöliittymäkomponenti minimi- ja maksimikokoa.

require 'Qt4'

class MyWidget < Qt::Widget
  def initialize(parent = nil)
    super
    setFixedSize(200, 120)

    quit = Qt::PushButton.new(tr('Poistu'), self)
    quit.setGeometry(62, 40, 75, 30)
    quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

    connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
  end
end

app = Qt::Application.new(ARGV)

widget = MyWidget.new()
widget.show()

app.exec()

Läpikäynti rivi riviltä

class MyWidget < Qt::Widget

Tässä luomme uuden luokan. Koska tämä luokka periytyy komponentista Qt::Widget, uusi luokka on käyttöliittymäkomponentti ja saattaa olla ylimmän tason ikkuna tai tytärkäyttöliittymäkomponentti (kuten Qt::PushButton edellisessä kappaleessa).

def initialize(parent = nil)
  super

Tällä luokalla on vain yksi jäsen, konstruktori (niiden jäsenien lisäksi, jotka se perii komponentilta Qt::Widget). Konstruktori on vakio Qt-käyttöliittymäkomponenttikonstruktori; sinun pitäisi aina käyttöliittymäkomponenttia luodessa lisätä samanlainen konstruktori.

Argumentti on sen emokäyttöliittymäkomponentti. Ylimmän tason ikkunan luomiseksi määrittele emokomponentiksi nil. Kuten voit nähdä, tämän käyttöliittymäkomponentin oletukset osoittavat sen olevan ylimmän tason ikkuna.

Kuten useimmat käyttöliittymäkomponentit, se vain välittää parent-parametrin Qt::Widget-konstruktorille. (Jos olet uusi Ruby-kielessä, niin kutsumalla super ilman mitään parametreja välitetään automaattisesti kaikki metodin vastaanottamat parametrit superluokan samannimiselle metodille. Update: Ilmeisesti on myös tärkeää jättää pois sulkeet — kutsumalla super() kutsutaan superluokan samanniminen metodi tyhjällä parametriluettelolla.)

    setFixedSize(200, 120)

Because this widget doesn't know how to handle resizing, we fix its size. In the next chapter, we will show how a widget can respond to resize event from the user.

quit = Qt::PushButton.new(tr('Quit'), self)
quit.setGeometry(62, 40, 75, 30)
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

Here we create and set up a child widget of this widget (the new widget's parent is self, i.e. the MyWidget instance).

The QObject::tr() function call around the string literal 'Quit' marks the string for translation, making it possible to change it at run-time based on the contents of a translation file. It is a good habit to use QObject::tr() around all user-visible strings, in case you decide later to translate your application to other languages.

The Qt::Widget::setGeometry() call sets both the widget's screen position and the size. It is equivalent to calling Qt::Widget::move() followed by Qt::Widget::resize().

connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))

The $qApp pointer is a global variable created when you require 'Qt'. It points to the application's unique Qt::Application instance.

app = Qt::Application.new(ARGV)

widget = MyWidget.new()
widget.show()

app.exec()

Here we instantiate our new child, set it to be the main widget, and execute the application.

Running the Application

This program is very similar in behavior to the previous one. The difference lies in the way we have implemented it. It does behave slightly differently, however. Just try to resize it to see.

Exercises

Try to create another MyWidget object. What happens?

Try to add more buttons or put in widgets other than Qt::PushButton.