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

From KDE TechBase
(Created page with "Kun [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter] täyttää nelikulmion, ympyrän, tai mitä hyvänsä, se täyttää muodon käyttäen sivellintään. Tässä aset...")
(Created page with "Funktio [http://doc.qt.nokia.com/latest/qpainter.html#translate Qt::Painter::translate()] kääntää [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter]-koordinaattijärj...")
Line 46: Line 46:
</syntaxhighlight>
</syntaxhighlight>


The [http://doc.qt.nokia.com/latest/qpainter.html#translate Qt::Painter::translate()] function translates the coordinate system of the [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter] (i.e., it moves it by an offset). Here we set the (0, 0) point to the bottom-left corner of the widget. The x and y directions remain unchanged, i.e., all the y coordinates inside the widget are now negative. (See [http://doc.qt.nokia.com/latest/coordsys.html The Coordinate System] for more information about Qt's coordinate system.)
Funktio [http://doc.qt.nokia.com/latest/qpainter.html#translate Qt::Painter::translate()] kääntää [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter]-koordinaattijärjestelmän (ts., se siirtää sen siirrososoitteella). Tässä asetamme koordinaatin (0, 0) osoittamaan käyttöliittymäkomponentin vasenta alakulmaa. Suunnat x ja y säilyvät muuttumattomina, ts., kaikki y-koordinaatit käyttöliittymäkomponentin sisällä ovat nyt negatiivisia. (Katso [http://doc.qt.nokia.com/latest/coordsys.html koordinaattijärjestelmästä] lisätietoja Qt:n koordinaattijärjestelmästä.)


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

Revision as of 17:53, 12 October 2011

Other languages:


Development/Tutorials/Qt4 Ruby Tutorial/Chapter 09


Kanuunalla kykenet
Tutorial Series   Qt4 Ruby -oppikurssi
Previous   Oppikurssi 8 - Taisteluun valmistautuminen
What's Next   Oppikurssi 10 - Sileää kuin silkki
Further Reading   n/a

Kanuunalla kykenet

Tiedostot:

Yleistä

Tässä esimerkissä meistä tulee graafikko piirtämällä pienen soman kanuunan. Vain cannon.rb eroaa edellisestä kappaleesta.

Läpikäynti rivi riviltä

cannon.rb

def paintEvent(event)
  painter = Qt::Painter.new(self)

Aloitamme nyt käyttämällä Qt::Painter tosissaan. Luomme taidemaalarin, joka toimii tällä käyttöliittymäkomponentilla.

    painter.setPen(Qt::NoPen)

Reunat, jotka Qt::Painter piirtää on piirretty käyttäen kynää. Tässä asetamme sen arvoon Qt::NoPen tarkoittaen, että ei ole mitään erityistä reunaa, kun piirrämme jotain.

    painter.setBrush(Qt::Brush.new(Qt::blue))

Kun Qt::Painter täyttää nelikulmion, ympyrän, tai mitä hyvänsä, se täyttää muodon käyttäen sivellintään. Tässä asetamme sen käyttämään kiinteää sinistä sivellintä. (Voisimme myös käyttää mallia.) Sininen sivellin menee koko matkan niiden asioiden reunoihin, jotka piirrämme.

    painter.translate(0, rect().height())

Funktio Qt::Painter::translate() kääntää Qt::Painter-koordinaattijärjestelmän (ts., se siirtää sen siirrososoitteella). Tässä asetamme koordinaatin (0, 0) osoittamaan käyttöliittymäkomponentin vasenta alakulmaa. Suunnat x ja y säilyvät muuttumattomina, ts., kaikki y-koordinaatit käyttöliittymäkomponentin sisällä ovat nyt negatiivisia. (Katso koordinaattijärjestelmästä lisätietoja Qt:n koordinaattijärjestelmästä.)

    painter.drawPie(Qt::Rect.new(-35, -35, 70, 70), 0, 90 * 16)

The Qt::Painter::drawPie() function draws a pie shape inside the specified rectangle using a start angle and an arc length. The angles are specified in sixteenths of a degree. Zero degrees is at the 3 o'clock position. The drawing direction is counter-clockwise. Here we draw a quarter of a circle in the bottom-left corner of the widget. The pie is filled with blue and has no outline.

    painter.rotate(-@currentAngle)

The Qt::Painter::rotate() function rotates the coordinate system of the Qt::Painter around the origin. The rotation argument is given in degrees (not given in sixteenths of a degree as above) and clockwise. Here we rotate the coordinate system @currentAngle degrees counter-clockwise.

    painter.drawRect(Qt::Rect.new(30, -5, 20, 10))

The Qt::Painter::drawRect() function draws the specified rectangle. Here we draw the barrel of the cannon.

It can often be difficult to envision the resulting drawing when the coordinate system has been transformed (translated, rotated, scaled, or sheared) as above.

In this case the coordinate system is first translated and then rotated. If the rectangle Qt::Rect.new(30, -5, 20, 10) had been drawn in the translated coordinate system, it would have looked like this:

Note that the rectangle is clipped by the border of the CannonField widget. When we rotate the coordinate system, for instance 60 degrees, the rectangle will be rotated around (0, 0), which is the bottom-left corner because we have translated the coordinate system. The result looks like this:

Sovelluksen suorittaminen

When the slider is operated the angle of the drawn cannon changes accordingly.

Harjoitukset

Aseta eri kynä eikä Qt::NoPen. Aseta kuvioitu sivellin.