座標の取得とシグナルの発生
Ruby/Qteの勉強 その29
画面のCanvasViewのフィールド内をタップすると、その場所に座標を表示します。元ネタは前回と同じくここで殆どそのまんまのスクリプトです。
前回と同様に内部のメソッド?の上書きを行うためにQCanvasViewではなくQRCanvasViewを使用。このクラスの中でシグナル用?のオブジェクト@tappedを作成して、画面をタップされたイベントcontentsMousePressEventでシグナルを発生させている(と思う)
タップされた座標はQPointのオブジェクト@penPosに保持される。
シグナルを受け取ったメソッドdispPosではQCanvasTextのオブジェクト@textをタップされた位置に移動、座標を表示させている。
#!/usr/bin/env ruby require "qte" require "qpe" require "qtecanvas" # 追加 include Qte include Qpe include Qtecanvas # 追加 class SampleWindow < QMainWindow def initialize() super() setCaption(tr("サンプル")) @msg = QLabel.new(tr("タップした位置に座標を表示"),self) @msg.setGeometry(0,0,310,35) @field = QCanvas.new( 640, 310 ) # キャンバス作成 @fieldView = FieldView.new( @field, self) # 表示枠作成 @fieldView.setGeometry(0,100,640,350) connect(@fieldView.tapped, self, 'dispPos') @text = QCanvasText.new(@field) end def dispPos # @text = QCanvasText.new(@field) x = @fieldView.penPos.x y = @fieldView.penPos.y @text.move(x, y) @text.setText("X=#{x}, Y=#{y}") # 座標から文字列作成 @text.show @field.update # フィールド更新 end end class FieldView < QRCanvasView # QCanvasViewではない def initialize(field, parent = nil, name = '') super(field, parent, name) @penPos = QPoint.new(0, 0) # 座標オブジェクト作成 @tapped = RSignal.new # 新規シグナル作成? catchEvent end attr_reader :tapped # 外部から読み取り可能にする attr_reader :penPos # QScrollViewのProtecedMember def contentsMousePressEvent(e) @penPos.setX(contentsX + e.x) # contentsX が不明 @penPos.setY(contentsY + e.y) @tapped.send # タップイベントでシグナル送信? end end $defaultCodec = QTextCodec.codecForName("utf8") app = QPEApplication.new([$0]+ARGV) app.setDefaultCodec($defaultCodec) QApplication.setFont(QFont.new("lcfont",18)) app.showMainWidget(SampleWindow.new) app.exec