Yo all -
Does anyone have good reasons for or against inheriting slot fns, rather than getting Qt to call derived them directly? E.g. which is better below, scenario A or scenario B?:
Scenario A: using C++ inheritance on a slot
==================================
class MyBaseA : public QObject
{
Q_OBJECT
. . .
public slots:
virtual void mySlot(); // overridable slot
};
class MyDerivedA : public MyBaseA
{
public:
void mySlot(); // this is NOT a slot, just an override of MyBaseA::mySlot()
};
MyBaseA::makeConnections()
{
// Connects to slot MyBaseA::mySlot(). However, MyDerivedA::mySlot()
// always gets called thanks to C++ inheritance.
connect( obj, SIGNAL(mySignal()), this, SLOT(mySlot()));
}
Scenario B: Qt calls the a slot directly
=============================
class MyBaseB : public QObject
{
Q_OBJECT
. . .
public slots:
void mySlot(); // ordinary slot
};
class MyDerivedB : public MyBaseB
{
Q_OBJECT // (incidentally, is this required ???!)
public slots:
void mySlot(); // ordinary slot
};
MyBaseB::makeConnections()
{
// Connects to either MyBaseB::mySlot() or MyDerivedB::mySlot(),
// depending on whether *this is MyBaseB or MyDerivedB.
connect( obj, SIGNAL(mySignal()), this, SLOT(mySlot()));
}
It seems to me that scenario A is superior: we only require a single slot in the base class - and so only a single Q_OBJECT framework (i.e. MOC file).
In scenario B, we have two independent moc frameworks (i.e. moc files), and it may not be obvious which of the two mySlot() fns is called by MyBaseB::makeConnections(): it all depends on whether *this is a pointer to a MyBaseB or to a MyDerivedB.
If anyone has anything to add, or can see something wrong in my argument, please reply! Cheers!