Slot in inherited class is not working...



  • I have an inherited class from a base class that is derived from QObject. Can someone tell me why my inherited class slot is not being invoked..

    @
    class cls_A : public QObject
    {
    Q_OBJECT

    private:
    explicit cls_A(QObject *parent = 0);

    // etc......
    

    }

    class cls_B : public cls_A
    {
    private:
    cls_B()
    {
    // instantiate Serial Device Enumerator and get available serial ports
    m_sde = new SerialDeviceEnumerator(this);
    connect(m_sde, SIGNAL(hasChanged(QStringList)), this, SLOT(GetDevices(QStringList)));
    m_sde->setEnabled(true);
    }

    private slots:
    void GetDevices(const QStringList &list)
    {
    // do something....
    }
    //......
    }
    @

    I run the application but GetDevices is not being invoked... Does cls_B have to have the Q_OBJECT declaration?



  • Yes, every class that inherits from QObject - regardless wheter direct or in an inheritance path - must include the Q_OBJECT macro in the private section of the class definition.



  • when I do place the Q_OBJECT macro, I get an error indicating ¨error: undefined reference to `vtable for cls_B¨... what does this error imply?



  • Did you re-run qmake? It must pick up the new header and include some additional build steps for a QObject class with the Q_OBJECT macro in it. You also must add the header file to the HEADERS variable in the .pro file.



  • Also, if you declare and implement classes in the same file filename, you'll need a

    @
    #include "filename.moc"
    @



  • hi i think i works for me but
    try to put the implement of the class constructor and other function in a cpp file not within the class
    and put Q_OBJECT for the two classes


Log in to reply
 

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