I’m trying to write a custom clock widget in Ruby Qt gem install qtbindings The clock should update every second with the current time. It doesn’t, the code seems perfect, why doesn’t this work?
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
It works if I replace the
connectcall withpaint_eventnever gets called and can be omitted. That gives the hint that the widget did not change, so we change it when the timer triggers.