0

我正在尝试在 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
4

1 回答 1

0

如果我connect

  @timer.connect(SIGNAL :timeout) {
    set_text(Time.now.strftime @date_format)
    update
  }

paint_event永远不会被调用,可以省略。这暗示了小部件没有改变,所以我们在定时器触发时改变它。

于 2012-10-26T20:01:23.587 回答