Search code examples
rubyqtqtruby

Ruby's subclassing and Qt's signals/slots don't work together as expected


When trying to work with Qt's signal/slot mechanisms over more than one level of inheritance, I ran into a problem: When my class does not directly inherit from QObject, signals and slots don't seem to work any more.

The output of the following program illustrates the case:

require 'Qt'

class A < Qt::Object
  signals 'mySignal()'
  slots 'mySlot()'

  def initialize
    super()
    puts "This is the c'tor of A and I am a #{self.class}"
    connect(self, SIGNAL('mySignal()'), self, SLOT('mySlot()'))
    emit mySignal()
  end

  def mySlot
    puts "Signal received and I am a #{self.class}"
  end
end

class B < A
  def initialize
    super()
  end
end

app = Qt::Application.new(ARGV)
A.new
B.new
app.exec

The program yields

This is the c'tor of A and I am a A
Signal received and I am a A
This is the c'tor of A and I am a B

However, I would expect

This is the c'tor of A and I am a A
Signal received and I am a A
This is the c'tor of A and I am a B
Signal received and I am a B

Qt' documentation states, that it "[...] assumes that the first inherited class is a subclass of QObject.". Since B < A < QObject, I would expect that to be true. The according C++ program behaves as expected (although you cannot identify the type of an object in its c'tor in c++, but that is besides the point here).

The question is: Why does the program not give the expected output?


Solution

  • As Terence Simpson pointed out, this is a bug that was still present in Qt Ruby 1.4.9. It got fixed in the meantime.