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

From KDE TechBase
(Created page with "Lisäämme toisen '''<tt>LCDRange</tt>'''-komponentin, jota käytetään voiman asettamiseen.")
(Created page with "Yhdistämme käyttöliittymäkomponentin '''<tt>force</tt>''' ja käyttöliittymäkomponentin '''<tt>cannonField</tt>''', aivan kuin teimme käyttöliittymäkomponentille '''<tt>...")
Line 129: Line 129:
</syntaxhighlight>
</syntaxhighlight>


We connect the '''<tt>force</tt>''' widget and the '''<tt>cannonField</tt>''' widget, just like we did for the '''<tt>angle</tt>''' widget.
Yhdistämme käyttöliittymäkomponentin '''<tt>force</tt>''' ja käyttöliittymäkomponentin '''<tt>cannonField</tt>''', aivan kuin teimme käyttöliittymäkomponentille '''<tt>angle</tt>'''.


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

Revision as of 12:18, 29 October 2011

Other languages:


Development/Tutorials/Qt4 Ruby Tutorial/Chapter 10


Sileää kuin silkki
Tutorial Series   Qt4 Ruby -oppikurssi
Previous   Oppikurssi 9 - Kanuunalla onnistuu
What's Next   Oppikurssi 11 - Annetaan sille laukaus
Further Reading   n/a

Sileää kuin silkki

Tiedostot:

Yleiskuva

Tässä esimerkissä lisäämme voimaa ohjaimeen.

Läpikäynti rivi riviltä

cannon.rb

Kentällä CannonField on nyt voima-arvo kulman lisäksi.

signals 'angleChanged(int)', 'forceChanged(int)'
slots 'setAngle(int)', 'setForce(int)'

Voiman rajapinta noudattaa samaa käytäntöä kuin kulma.

def initialize(parent = nil)
  super()

  @currentAngle = 45
  @currentForce = 0

  setPalette(Qt::Palette.new(Qt::Color.new(250, 250, 200)))
  setAutoFillBackground(true)
end

Voima @currentForce on alustettu nollaksi.

def setAngle(angle)
  if angle < 5
    angle = 5
  elsif angle > 70
    angle = 70
  end

  if @currentAngle == angle
    return
  end

  @currentAngle = angle
  update(cannonRect())
  emit angleChanged(@currentAngle)
end

Teimme pienen muutoksen setAngle()-funktioon. Se piirtää uudelleen käyttöliittymän osan, joka sisältää kanuunan.

def setForce(force)
  if force < 0
    force = 0
  end
  if @currentForce == force
    return
  end

  @currentForce = force
  emit forceChanged(@currentForce)
end

setForce()-toteutus on aika samanlainen kuin setAngle(). Ainoa erilaisuus on, että koska emme näytä voima-arvoa, käyttöliittymäkomponenttia ei tarvitse piirtää uudelleen.

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

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

  painter.translate(0, height())
  painter.drawPie(Qt::Rect.new(-35, -35, 70, 70), 0, 90 * 16)
  painter.rotate(-@currentAngle)
  painter.drawRect(Qt::Rect.new(30, -5, 20, 10))
  painter.end()
end

Piirrämme kuin kappaleessa 9.

def cannonRect()
  result = Qt::Rect.new(0, 0, 50, 50)
  result.moveBottomLeft(rect().bottomLeft())
  return result
end

Funktio cannonRect() palauttaa nelikulmion, joka sisältää kanuunan sisältävän käyttöliittymäkomponentin. Luomme aluksi nelikulmion, jonka koko on 50 x 50 ja siirrämme sen niin, että sen vasen alakulma on sama kuin käyttöliittymäkomponentin vasen alakulma.

Funktio Qt::Widget::rect() palauttaa käyttöliittymän sisältämän nelikulmion käyttöliittymän omina koordinaatteina. Nelikulmion vasen yläkulma on aina (0, 0).

t10.rb

Konstruktori on enimmäkseen sama, mutta joitakin uusia bittejä on lisätty.

force = LCDRange.new()
force.setRange(10, 50)

Lisäämme toisen LCDRange-komponentin, jota käytetään voiman asettamiseen.

connect(force, SIGNAL('valueChanged(int)'),
         cannonField, SLOT('setForce(int)'))
connect(cannonField, SIGNAL('forceChanged(int)'),
         force, SLOT('setValue(int)'))

Yhdistämme käyttöliittymäkomponentin force ja käyttöliittymäkomponentin cannonField, aivan kuin teimme käyttöliittymäkomponentille angle.

leftLayout = Qt::VBoxLayout.new()
leftLayout.addWidget(angle)
leftLayout.addWidget(force)

gridLayout = Qt::GridLayout.new()
gridLayout.addWidget(quit, 0, 0)
gridLayout.addLayout(leftLayout, 1, 0)
gridLayout.addWidget(cannonField, 1, 1, 2, 1)
gridLayout.setColumnStretch(1, 10)

In Chapter 9, we put angle in the lower-left cell of the layout. Now we want to have two widgets in that cell, so we make a vertical box, put the vertical box in the grid cell, and put each of angle and range in the vertical box.

force.setValue(25)

We initialize the force value to 25.

Running the Application

We now have a force control.

Exercises

Make the size of the cannon barrel be dependent on the force.

Put the cannon in the bottom-right corner.

Try adding a better keyboard interface. For example, make + and - increase and decrease the force and enter shoot. If you're bothered by the way the Left and Right keys work, change that too. [Hint: Reimplement Qt::Widget::keyPressEvent().]