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?

Tom
  • 1,096
  • 1
  • 10
  • 15
  • Does the connect (in particular, the call from where A's ctor is called by B's) fail? If so, the connection isn't properly being made, so the signal wouldn't call the slot. – strager Jan 05 '09 at 21:13
  • The connection seems to be properly established. At least Qt doesn't complain as it usually does if you try using signals or slots that do not exist. – Tom Jan 06 '09 at 06:25
  • I ran your example and got the output you expected, ie the "Signal received and I am a B" line. (Ubuntu Jaunty(9.04)) – Terence Simpson May 24 '09 at 12:44
  • Perhaps this is a bug that got fixed. Which version of QtRuby are you using? – Tom May 25 '09 at 04:53
  • ruby1.8: 1.8.7.72-3 libqt4-ruby1.8: 4:4.2.2-0ubuntu2 – Terence Simpson May 25 '09 at 08:48
  • Ok, I am using 1.4.9. Apparently this is a bug that got fixed. I will close the question. – Tom Jun 23 '09 at 12:30

2 Answers2

1

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
  • 3,677
  • 1
  • 27
  • 28
  • Thanks for answering. I have tested the whole thing in C++ and included the findings in the question. – Tom Jan 05 '09 at 21:00
0

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.

Tom
  • 1,096
  • 1
  • 10
  • 15