PDA

View Full Version : Connecting to a base class signal?



AaronMK
26th October 2007, 22:53
I am trying to connect to a signal in a base class. The pointer is the type of the base class but points to an object of the derived class. My classes are declared similar to the following:



class Base : public QObject
{
Q_OBJECT
public:
Base();
signals:
void BaseSignal(char Arg);
};

class DerivA : public Base
{
Q_OBJECT
public:
DerivA();

void Alert() { emit BaseSignal('A') };
};

class DerivB : public Base
{
Q_OBJECT
public:
DerivB();

void Alert() { emit BaseSignal('B') };
};


Connect fails if I pass a DerivA or DerivB pointer to a function like the following:



bool SomeObject::mbrConnect(Base *B)
{
return QObject::connect(B, SIGNAL(BaseSignal(char)), this, SLOT(Handler(char));
}


It seems it is too smart for its own good, being able to identify the real type of the Base pointer in the Connect Fail output message. For example, passing mbrConnect a DerivA pointer would output the following:


Object::connect: No such signal DerivA::BaseSignal(char)

How do I solve this?

marcel
26th October 2007, 23:24
This happens because of moc. Only the metaobject of Base contains the implementation of the signal.
Since the declarations of DerivA and B do not declare any signals, their metaobjects do not implement the signal, nor do they inherit from the Base metaobject.

Solution: add the signal declaration to both derived classes and remove it from the base class.

jacek
26th October 2007, 23:33
I am trying to connect to a signal in a base class.[...]
How do I solve this?
It should work. Make sure that moc is run on each of these classes and try recompiling your application.


Since the declarations of DerivA and B do not declare any signals, their metaobjects do not implement the signal, nor do they inherit from the Base metaobject.
But still meta objects of derived classes have access to meta object of the base class:

const QMetaObject DerivB::staticMetaObject = {
{ /* --> */ &Base::staticMetaObject /* <-- */, qt_meta_stringdata_DerivB,
qt_meta_data_DerivB, 0 }
};

jpn
26th October 2007, 23:33
Solution: add the signal declaration to both derived classes and remove it from the base class.
Nah, something else must be wrong. ;) One can fairly well emit signals declared in base classes. One can also establish signal-slot connections by simply passing a plain QObject pointer, it doesn't have to be of that type declaring such signal or slot.

jacek
26th October 2007, 23:37
Nah, something else must be wrong.
Yes, maybe a typo in signal name?