Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

signals in a derived class ?



  • I have a base class which has signals:

    class MsgDecoder : public QObject
    {
       Q_OBJECT
     
    private:
       QString mstrMsgType;
       eDestination meDest;
       static tMsgTypesMap msmpMsgTypes;
     
    public:
       MsgDecoder(const QString& crstrMsgType, const eDestination ceDest);
     
       virtual bool blnDecode(const QJsonObject& crobjMsg) = 0;
       QString cstrMsgType() const { return mstrMsgType; }
       static bool blnDecodeMsgByType(const QString& crstrMsgType,
                                      const QJsonObject& crobjMsg);
       eDestination eDest() { return meDest; }
       static eDestination getDestFromMsg(const QJsonObject& crobjMsg);
       static eDestination getDestFromMsg(const QString& crstrMsg);
       static eDestination getDestFromMsgType(const char* cpszMsgType);
     
    signals:
       void error(const QString& crstrErrorMsg);  
    };
    

    I have several working derived classes based on the above, today I've added a new class:

    class SessionMsgRestarted : public MsgDecoder
    {
    public:
        SessionMsgRestarted();
        virtual bool blnDecode(const QJsonObject &crobjMsg);
     
    signals:
        void traineeRestarted(const QString& crstrMACaddress);
    };
    

    However I'm getting very strange compilation errors which I cannot see what the cause is:

    sessionmsg.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall SessionMsgRestarted::traineeRestarted(class QString const &)" (?traineeRestarted@SessionMsgRestarted@@QAEXABVQString@@@Z) referenced in function "public: virtual bool __thiscall SessionMsgRestarted::blnDecode(class QJsonObject const &)" (?blnDecode@SessionMsgRestarted@@UAE_NABVQJsonObject@@@Z)
    debug\trngServer.exe:-1: error: LNK1120: 1 unresolved externals
    

    I'm just wondering is there any restriction or limitation on signals in derived classes?



  • @SPlatten
    Your class SessionMsgRestarted does not specify Q_OBJECT. Even though derived from a class which does, I believe you must specify this to declare a signal in any class? And then don't forget to do a change-underpants rebuild when you add that macro to an existing previously-compiled class :)



  • @SPlatten
    Your class SessionMsgRestarted does not specify Q_OBJECT. Even though derived from a class which does, I believe you must specify this to declare a signal in any class? And then don't forget to do a change-underpants rebuild when you add that macro to an existing previously-compiled class :)



  • I think you still need to have a Q_OBJECT line in your derived class



  • @SPlatten said in signals in a derived class ?:

    class SessionMsgRestarted : public MsgDecoder
    {
    public:

    No Q_OBJECT macro in derived class?!

    Edit:

    Hahah @JonB @mchinand :)
    3 instant replies



  • @JonB , thank you, I thought you had the solution, but now I get:

    sessionmsg.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall SessionMsgRestarted::metaObject(void)const " (?metaObject@SessionMsgRestarted@@UBEPBUQMetaObject@@XZ)
    sessionmsg.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall SessionMsgRestarted::qt_metacast(char const *)" (?qt_metacast@SessionMsgRestarted@@UAEPAXPBD@Z)
    sessionmsg.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall SessionMsgRestarted::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@SessionMsgRestarted@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    sessionmsg.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall SessionMsgRestarted::traineeRestarted(class QString const &)" (?traineeRestarted@SessionMsgRestarted@@QAEXABVQString@@@Z) referenced in function "public: virtual bool __thiscall SessionMsgRestarted::blnDecode(class QJsonObject const &)" (?blnDecode@SessionMsgRestarted@@UAE_NABVQJsonObject@@@Z)
    debug\trngServer.exe:-1: error: LNK1120: 4 unresolved externals
    


  • @SPlatten
    And did you do the change-underpants rebuild like I said not to forget? That means: delete the build output directory's files and rebuild.....



  • @JonB , actually, I just did a clean, then rebuild all and now its all good, thank you!



  • @SPlatten
    Basically, if you add (or presumably also remove) Q_OBJECT to a class which you previously compiled without it, you need to force a full rebuild every time. I do it via deleting all the files as I thought some peeps say rebuild alone is not enough, but I don't know. And any time I get a Qt link error which I don't expect/impenetrable message and looks vaguely like it might be related to objects/metas etc. I do that before I start wondering if I have made some other mistake :)



  • @JonB said in signals in a derived class ?:

    Basically, if you add (or presumably also remove) Q_OBJECT to a class which you previously compiled without it, you need to force a full rebuild every time. I do it via deleting all the files as I thought some peeps say rebuild alone is not enough, but I don't know. And any time I get a Qt link error which I don't expect/impenetrable message and looks vaguely like it might be related to objects/metas etc. I do that before I start wondering if I have made some other mistake :)

    When changing Q_OBJECT and/or signals/slots, you have to rerun the MOC. When you are lucky QtCreator detect those changes and rerun himself the MOC.

    With qmake based projects, you can rerun qmake which will rerun the MOC and update the makefile and then rebuild the project.

    Width cmake projects, I don't know, I have no experience.

    At least, if nothing works, deleting the build directory is the solution which always works ;)



  • @JonB said in signals in a derived class ?:

    I do that before I start wondering if I have made some other mistake :)

    "Clean & (Re-)Build" and "qmake" is like being root on Linux or rebooting your Windows machine :-)
    Fixes a lot of errors ;-)


Log in to reply