[SOLVED]C++ slot in QML



  • Hi!
    I have abstract class, let it be IParent, with this structure:
    @class IParent : public QObject
    {
    Q_OBJECT
    public slots:
    virtual void parentSlot() = 0;
    };@

    And its child Child:
    @class Child : public IParent
    {
    public slots:
    void parentSlot() = {//slot definition};
    void childSlot() = {//slot definition};
    };@

    In main.cpp Child class registed as
    @qmlRegisterType<Child>("package", 1, 0, "Child");@

    In QML file when I try to execute childSlot() I get this error:
    @TypeError: Property 'childSlot' of object IParent (0x3625880) is not a function@

    How it is possible to fix it and point QML to execute Child slot instead of IParent?


  • Lifetime Qt Champion

    Hi,

    The thing I currently see is that your are missing the Q_OBJECT macro in your Child class



  • Is it necessary to use Q_OBJECT macro, if it exists in parent class?

    In real application I have IBluetoothProvider as parent class and SppProvider and BlueEvaProvider as its children. SppProvider does not have Q_OBJECT macro or new slots, it is only defines IBluetoothProvider methods and works fine:)

    Adding Q_OBJECT to BlueEvaProvider class gives me errors like:
    LNK2001 and LNK2019 - unresolved external symbol

    Full error output:
    @main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall BlueEvaProvider::metaObject(void)const " (?metaObject@BlueEvaProvider@@UBEPBUQMetaObject@@XZ)
    blueevaprovider.obj : error LNK2019: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall BlueEvaProvider::metaObject(void)const " (?metaObject@BlueEvaProvider@@UBEPBUQMetaObject@@XZ) referenced in function "private: virtual void __thiscall BlueEvaProvider::receiveData(void)" (?receiveData@BlueEvaProvider@@EAEXXZ)
    main.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall BlueEvaProvider::qt_metacast(char const *)" (?qt_metacast@BlueEvaProvider@@UAEPAXPBD@Z)
    blueevaprovider.obj : error LNK2019: unresolved external symbol "public: virtual void * __thiscall BlueEvaProvider::qt_metacast(char const *)" (?qt_metacast@BlueEvaProvider@@UAEPAXPBD@Z) referenced in function "public: void __thiscall BlueEvaProvider::availablePorts(void)" (?availablePorts@BlueEvaProvider@@QAEXXZ)
    main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall BlueEvaProvider::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@BlueEvaProvider@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    blueevaprovider.obj : error LNK2019: unresolved external symbol "public: virtual int __thiscall BlueEvaProvider::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@BlueEvaProvider@@UAEHW4Call@QMetaObject@@HPAPAX@Z) referenced in function "public: void __thiscall BlueEvaProvider::availablePorts(void)" (?availablePorts@BlueEvaProvider@@QAEXXZ)
    main.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const BlueEvaProvider::staticMetaObject" (?staticMetaObject@BlueEvaProvider@@2UQMetaObject@@B)
    blueevaprovider.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const BlueEvaProvider::staticMetaObject" (?staticMetaObject@BlueEvaProvider@@2UQMetaObject@@B)@


  • Lifetime Qt Champion

    Yes, all classes that derives from QObject and are using signals and slots must include the Q_OBJECT macro.

    You must re-run qmake when adding/removing that macro from a class



  • Oh, yes, it is really helped! Thanks a lot!


Log in to reply
 

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