Skip to content

Instantly share code, notes, and snippets.

@syun77
Last active December 11, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syun77/4622876 to your computer and use it in GitHub Desktop.
Save syun77/4622876 to your computer and use it in GitHub Desktop.
QtRubyサンプル
# ■ボタン表示
require 'rubygems'
require 'Qt'
# アプリケーション生成
app = Qt::Application.new(ARGV)
# ウィジット生成
window = Qt::Widget.new()
# サイズを200x120に
window.resize(200, 120)
# ボタン生成
quit = Qt::PushButton.new("Quit", window)
# ボタン配置
quit.setGeometry(10, 40, 180, 40)
Qt::Object.connect(quit, SIGNAL("clicked()"), app, SLOT("quit()"))
# ボタン表示
window.show()
# 開始
app.exec()
# ■QtRubyのリストボックス表示
require 'rubygems'
require 'Qt'
# アプリケーション生成
app = Qt::Application.new(ARGV)
# リストボックス生成
list = Qt::ListWidget.new
list.clear
# 項目追加
list.addItem("hoge")
list.addItem("piyo")
list.addItem("momo")
list.addItem("toro")
list.addItem("iwashi")
list.addItem("saba")
# リストボックス表示
list.show
# 開始
app.exec()
# ■テキストボックスの入力内容を、ラベルに反映
require 'rubygems'
require 'Qt'
# アプリケーション生成
app = Qt::Application.new(ARGV)
# ラベル生成
label = Qt::Label.new("Hello")
# テキストボックス生成
edit = Qt::LineEdit.new
# ウィンドウ生成
window = Qt::Widget.new
# レイアウト生成
layout = Qt::HBoxLayout.new;
# テキストボックスの入力内容をラベルに関連付け
Qt::Object.connect(edit,SIGNAL("textChanged(QString)"),
label,SLOT("setText(QString)") )
# テキストボックスの入力内容をウィンドウタイトルに反映
Qt::Object.connect(edit,SIGNAL("textChanged(QString)"),
window,SLOT("setWindowTitle(QString)") )
# 配置
layout.addWidget(edit)
layout.addWidget(label)
window.setLayout(layout)
window.show()
# アプリケーション実行
app.exec()
# ■QtRubyでテキストボックス表示
require 'rubygems'
require 'Qt'
# アプリケーション生成
app = Qt::Application.new(ARGV)
# テキストボックス生成
lEdit = Qt::LineEdit.new
# 最大8文字まで
lEdit.setMaxLength(8)
# テキストボックス表示
lEdit.show
# 開始
app.exec()
# ■クラスを使って実装
require 'rubygems'
require 'Qt'
class MainWindow < Qt::MainWindow
def initialize
super
# タイトル文字を設定
self.window_title = "Test"
# 640 x 480 にする
resize(640, 480)
end
end
app = Qt::Application.new(ARGV)
window = MainWindow.new
window.show
app.exec
# ■ボタン呼び出しで特異メソッド呼び出し
require 'rubygems'
require 'Qt'
class MainWidget < Qt::Widget
def initialize
super
button = Qt::PushButton.new("Test")
label = Qt::Label.new("Hello")
# ボタンを押したら hoge() を呼び出す
connect(button, SIGNAL("clicked()"), self, SLOT("hoge()"))
layout = Qt::VBoxLayout.new
layout.add_widget(button)
layout.add_widget(label)
self.setLayout(layout)
end
# 特異メソッド定義
slots "hoge()"
def hoge()
p "hoge"
end
end
class MainWindow < Qt::MainWindow
def initialize
super
# タイトル文字を設定
self.window_title = "Test"
# 160 x 120 にする
resize(160, 120)
@window = MainWidget.new
setCentralWidget(@window)
end
end
app = Qt::Application.new(ARGV)
window = MainWindow.new
window.show
app.exec
#!ruby -Ku
# ■モーダルダイアログの表示
require 'rubygems'
require 'Qt'
# メインウィンドウ生成
class MyWidget < Qt::Widget
slots 'onBtnClickMeClicked()'
# コンストラクタ
def initialize
super
create
end
# 生成
def create
# タイトル文字設定
self.setWindowTitle("Hello, world")
# (x,y,w,h)
self.setGeometry(300, 300, 250, 150)
self.setToolTip("Simple Widget")
vlay = Qt::VBoxLayout.new
# GUI生成
@btnClickMe = Qt::PushButton.new("Open")
@lblText = Qt::Label.new("empty")
# SIGNAL / SLOT 接続
connect(@btnClickMe, SIGNAL("clicked()"), SLOT("onBtnClickMeClicked()"))
# GUI配置
vlay.addWidget(@btnClickMe)
vlay.addWidget(@lblText)
setLayout(vlay)
end
# ボタンイベント
def onBtnClickMeClicked
# モーダルなので、閉じるまで次のステップには進まない
subWindow = SubWindow.new
case subWindow.exec()
when 1
# OKボタン(accept)を押したら、テキスト内容を取得
str = subWindow.getLineEditText()
@lblText.setText(str)
when 0
# キャンセルボタン(reject)をおしたら何もしない
p "nothing to do."
end
end
end
# サブウィンドウ(モーダル)
class SubWindow < Qt::Dialog
# slots 'onBtnClickMeClicked'
# コンストラクタ
def initialize
super
create
end
# 生成
def create
# タイトル文字列設定
setWindowTitle("Dialog")
vlay = Qt::VBoxLayout.new
# GUI生成
@btnOk = Qt::PushButton.new("ok")
@btnCancel = Qt::PushButton.new("cancel")
@editline = Qt::LineEdit.new("empty")
# SIGNAL / SLOT を接続
connect(@btnOk, SIGNAL("clicked()"), self, SLOT("accept()"))
connect(@btnCancel, SIGNAL("clicked()"), self, SLOT("reject()"))
# GUIを配置
vlay.addWidget(@btnOk)
vlay.addWidget(@btnCancel)
vlay.addWidget(@editline)
setLayout(vlay)
end
# テキストに入力した値を取得
def getLineEditText()
return @editline.text
end
end
app = Qt::Application.new(ARGV)
window = MyWidget.new
window.show
app.exec
#!ruby -Ku
# ■リストで選択した文字を取得
require 'rubygems'
require 'Qt'
# メインウィンドウ生成
class MyWidget < Qt::Widget
slots 'onBtnClickMeClicked()'
# コンストラクタ
def initialize
super
create
end
# 生成
def create
# タイトル文字設定
self.setWindowTitle("Hello, world")
# (x,y,w,h)
self.setGeometry(300, 300, 250, 150)
self.setToolTip("Simple Widget")
vlay = Qt::VBoxLayout.new
# GUI生成
@btnClickMe = Qt::PushButton.new("Open")
@lblText = Qt::Label.new("empty")
# SIGNAL / SLOT 接続
connect(@btnClickMe, SIGNAL("clicked()"), SLOT("onBtnClickMeClicked()"))
# GUI配置
vlay.addWidget(@btnClickMe)
vlay.addWidget(@lblText)
setLayout(vlay)
end
# ボタンイベント
def onBtnClickMeClicked
# モーダルなので、閉じるまで次のステップには進まない
subWindow = SubWindow.new
case subWindow.exec()
when 1
# OKボタン(accept)を押したら、テキスト内容を取得
str = subWindow.getListText
@lblText.setText(str)
when 0
# キャンセルボタン(reject)をおしたら何もしない
p "nothing to do."
end
end
end
# サブウィンドウ(モーダル)
class SubWindow < Qt::Dialog
# slots 'onBtnClickMeClicked'
# コンストラクタ
def initialize
super
create
end
# 生成
def create
# タイトル文字列設定
setWindowTitle("Dialog")
vlay = Qt::VBoxLayout.new
# GUI生成
@btnOk = Qt::PushButton.new("ok")
@btnCancel = Qt::PushButton.new("cancel")
@list = Qt::ListWidget.new
@list.addItem("hoge")
@list.addItem("piyo")
# SIGNAL / SLOT を接続
connect(@btnOk, SIGNAL("clicked()"), self, SLOT("accept()"))
connect(@btnCancel, SIGNAL("clicked()"), self, SLOT("reject()"))
# GUIを配置
vlay.addWidget(@btnOk)
vlay.addWidget(@btnCancel)
vlay.addWidget(@list)
setLayout(vlay)
end
# リストで選択した文字を取得
def getListText()
item = @list.currentItem
if(item)
return item.text
end
return ""
end
end
app = Qt::Application.new(ARGV)
window = MyWidget.new
window.show
app.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment