Daniel Irvine on building software
Timed events with Ruby and qtbindings
3 June 2014
In the Qt world, events are raised via signals and are handled by slots. Raising events at specific time intervals must be done through this mechanism: the
timeout() signal is the one we’re after.
With the latest qtbindings for Ruby you can reference signals and slots with the more Ruby-esque symbols:
timer = Qt::Timer.new(self) connect(timer, SIGNAL(:timeout), self, SLOT(:time_up)) time.start(1000)
The call to class method
slots then becomes
You can tie this in with a Ruby
Proc to build an abstraction around the Qt framework.
class MyWidget < Qt::Widget attr_writer :on_time_up slots :time_up def initialize @timer = Qt::Timer.new(self) connect(@timer, SIGNAL(:timeout), self, SLOT(:play)) @timer.start(1000) end def time_up @on_time_up.call end end