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

From KDE TechBase
(Created page with "Пушка детям не игрушка")
(Updating to match new version of source page)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<languages />
<languages />


{{Template:I18n/Language Navigation Bar|Development/Tutorials/Qt4 Ruby Tutorial/Chapter 09}}
 


{{TutorialBrowser/ru|
{{TutorialBrowser/ru|
Line 9: Line 9:
next=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_10|Пример 10: Гладкий как шёлк]]
next=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_10|Пример 10: Гладкий как шёлк]]
}}
}}
== With Cannon You Can ==
== Пушка детям не игрушка ==
[[Image:Qt4_Ruby_Tutorial_Screenshot_9.png|center]]
[[Image:Qt4_Ruby_Tutorial_Screenshot_9.png|center]]
Файлы:
Файлы:

Latest revision as of 14:24, 18 July 2012

Other languages:


Пушка детям не игрушка
Серия примеров   Введение в программирование на Qt®4 на языке Ruby
Необходимо знать   Пример 8: Готовься к битве
Следующий пример   Пример 10: Гладкий как шёлк
Литература   нет

Пушка детям не игрушка

Файлы:

Обзор

In this example we become graphic by drawing a cute little blue cannon. Only cannon.rb differs from the previous chapter.

Построчный обзор программы

cannon.rb

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

We'll now start to use Qt::Painter in earnest. We create a painter that operates on this widget.

    painter.setPen(Qt::NoPen)

The edges of what Qt::Painter draws are drawn using the pen. Here we set it to Qt::NoPen, meaning that there will be no special edge when we draw something.

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

When Qt::Painter fills a rectangle, a circle, or whatever, it fills the shape using its brush. Here we set it to use a solid blue brush. (We could also use a pattern.) The blue brush will go all the way to the edges of the things we draw.

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

The Qt::Painter::translate() function translates the coordinate system of the 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 The Coordinate System for more information about Qt's coordinate system.)

    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:

Запуск приложения

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

Exercises

Set a different pen instead of Qt::NoPen. Set a patterned brush.