Unsolved Connect with derived class
-
I have a base class:
class QCcmStatusPanel : public QFrame { Q_OBJECT public: explicit QCcmStatusPanel(QWidget *parent = 0); ~QCcmStatusPanel(); private: QOverflowCtrl* mOverflowCtrl; public slots: void OverflowSlot(int overflow); };
and a derived class
class QCcmTimeDomainStatusPanel : public QCcmStatusPanel { public: QCcmTimeDomainStatusPanel(QWidget *parent = 0); ~QCcmTimeDomainStatusPanel(); private: QDistributorDepthCtrl* mDistributorDepthCtrl; public slots: void DistributorDepthSlot(int distributorDepth); };
in my application I instantiate the class as
mStatusPanel = new QCcmTimeDomainStatusPanel(this); connect(distributor, SIGNAL(SetDepthSignal(int)), mStatusPanel, SLOT(DistributorDepthSlot(int)));
and I get a message
QObject::connect: No such slot QCcmStatusPanel::DistributorDepthSlot(int)
complaining that the base class doesn't have the slot that is defined in the base class.Is there something I have to do in connect for this to work?
-
Hi! Whenever you use signals and slots in a class you need to add the
Q_OBJECT
. -
@nefarious mStatusPanel is a QCcmStatusPanel, right? Since QCcmStatusPanel does not have DistributorDepthSlot slot you get that error. You need to cast the pointer mStatusPanel to QCcmTimeDomainStatusPanel* in the connect call (http://doc.qt.io/qt-5.7/qobject.html#qobject_cast).
-
@jsulm where are you seeing the code for your comment??? I agree with @Wieland that the Q_OBJECT needs to be in QCcmTimeDomainStatusPanel.
-
@Buckwheat In @nefarious first post
-
Hi
If i put his code in a .h file I get
"Error: Class declaration lacks Q_OBJECT macro."
for QCcmTimeDomainStatusPanel
(mingw compiler) -
@jsulm ... I still don't see the declaration for mStatusPanel. I think it is being assumed in the argument. I am not saying you are wrong on the intended declaration but I still believe the problem is the missing Q_OBJECT.
-
@Buckwheat My assumption is based on the error message:
QObject::connect: No such slot QCcmStatusPanel::DistributorDepthSlot(int)
-
@jsulm ... so was mine and @Wieland. But I use SIGNAL()/SLOT() (older method) with base classes all the time without issue unless I forget my Q_OBJECT or Q_GADGET.