hakeの日記

Windows環境でプログラミングの勉強をしています。

テキストを取得

Ruby/Qteの勉強 その4


ラベルで書いた文字やテキストボックス内の文字を取得するのには.text.local8Bit.to_strを用いる。文字が半角英数字のみの場合は.text.to_strでも可です。

#!/usr/bin/env ruby

require "qte"
require "qpe"
include Qte
include Qpe


class SampleWindow < QMainWindow
   def initialize()
      super()
      setCaption(tr("サンプル"))
      msg = QLabel.new(tr("これはサンプルプログラム"),self)
      msg.setGeometry(10,10,300,30)

      @ebox1 = QMultiLineEdit.new(self)
      @ebox1.setGeometry(0,160,635,120)
      @ebox1.setText(tr("あいうえお\nかきくけこ"))

      @ebox2 = QMultiLineEdit.new(self)
      @ebox2.setGeometry(0,280,635,120)

       naiyou = msg.text.local8Bit.to_str       #msgの文字をnaiyouに代入
#       naiyou = @ebox1.text.local8Bit.to_str   #@ebox1の文字をnaiyouに代入
       @ebox2.setText(tr(naiyou))               #@ebox2にnaiyouの内容を書く
   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
naiyou = @ebox1.text.local8Bit.to_str

は下記のようにしてもOK(らしい?)

naiyou = $defaultCodec.fromUnicode(@ebox1.text)