Monday, April 13, 2009

Refactoring QtRuby Example #1

On newbie seminar (see last post), I learned a structure of Qt Widgets. It's made from three parts: entry point, top widget class, and UI form class.

Look back my samples, these are not subdivided at all. So I refactor, for example, default.rb generated from rbuic4 command. I subdivide this script to 3 scripts.

ui.rb(UI form class)
require 'Qt4'

module UI
class DefaultClass < Qt::Object
# generated attr_reader properties

def setup_ui(mainWindow)
# generated codes
end

private
def retranslate_ui(mainWindow)
# generated codes
end

alias :setupUi :setup_ui
alias :retranslateUi :retranslate_ui
end
end

default.rb(top widget class)
require 'Qt4'
require 'default/ui'

class Default < Qt::MainWindow
attr_reader :ui

def initialize(parent=nil)
@ui = UI::DefaultClass.new
super
@ui.setup_ui(self)
ObjectSpace.define_finalizer(self, Default.delete_ui(@ui))
end

private
def Default.delete_ui(ui_to_delete)
proc {ui_to_delete.dispose unless ui_to_delete.disposed?}
end
end

main.rb(entry point)
require 'Qt4'
require 'default/default'

Qt::Application.new(ARGV) do
window = Default.new
window.show
exec
end

More details, to be continued...

No comments:

Post a Comment