我正在尝试在 Ruby Qt 中编写一个自定义时钟小部件gem install qtbindings
时钟应该每秒更新一次当前时间。它没有,代码看起来很完美,为什么这不起作用?
require "Qt4"
class ClockLabel < Qt::Label
attr_accessor :timer
def initialize(date_format = '%Y-%d-%mT%H:%M:%S')
super()
@date_format = date_format
self.set_text(Time.now.strftime date_format)
end
def paint_event(event)
super(event)
painter = Qt::Painter.new(self)
painter.draw_text(event.rect.x, event.rect.y, Time.now.strftime(@date_format))
end
def start
@timer = Qt::Timer.new(self)
self.connect(@timer, SIGNAL('timeout()'), self, SLOT('update()'))
@timer.start(1000)
end
end
if __FILE__ == $PROGRAM_NAME
app = Qt::Application.new([])
clock = ClockLabel.new()
clock.start
clock.show
app.exec()
end