Connect with derived class

  • I have a base class:

    class QCcmStatusPanel  : public QFrame
        explicit QCcmStatusPanel(QWidget *parent = 0);
        QOverflowCtrl* mOverflowCtrl;
    public slots:
        void OverflowSlot(int overflow);

    and a derived class

    class QCcmTimeDomainStatusPanel
            : public QCcmStatusPanel
        QCcmTimeDomainStatusPanel(QWidget *parent = 0);
        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.

  • Moderators

    @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 (

  • @jsulm where are you seeing the code for your comment??? I agree with @Wieland that the Q_OBJECT needs to be in QCcmTimeDomainStatusPanel.

  • Moderators

    @Buckwheat In @nefarious first post

  • Qt Champions 2017

    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.

  • Moderators

    @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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.