tags:

views:

764

answers:

2

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?

+1  A: 

To be able to utilize signals and slots - or more importantly the meta object system in Qt, the class has to inherit from QObject - and it has to inherit QObject first in multiple inheritance. See also http://doc.trolltech.com/4.4/moc.html for a good read on the meta object system

Henrik Hartz
Thanks for answering. I have tested the whole thing in C++ and included the findings in the question.
A: 

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.