Twitter GitHub Facebook Instagram dirv.me

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

slots :time_up

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

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram dirv.me