# Difference between revisions of "Archive:Development/Tutorials/Qt4 Ruby Tutorial/Chapter 10 (zh TW)"

##  Smooth as Silk

### 一行一行的瀏覽

CannonField 現在除了角度外，又加入力量值。

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

def initialize(parent = nil)
super()

@currentAngle = 45
@currentForce = 0

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

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

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

@currentForce = force
emit forceChanged(@currentForce)
end

setForce() 的實現相當類似 setAngle()。唯一的差別是，因為我們沒有顯示力量值，所以不需要重繪 widget。

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

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

cannonRect() 函式返回 widget 坐標中封裝加農砲的矩形。首先，我們建立一個大小50×50的矩形。然後移動它，所以它的左下角等同於 widget 自己的左下角。

Qt::Widget::rect() 函式返回在 widget 坐標中封裝 widget 自己矩形。矩形的左上角永遠是（0，0）。

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

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

leftLayout = Qt::VBoxLayout.new()

gridLayout = Qt::GridLayout.new()
gridLayout.setColumnStretch(1, 10)

force.setValue(25)