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

Problem with Q_GADGET



  • hi
    I have a little problem with Q_GADGET macro. Following example:
    @class A
    {

    };

    class B: public A
    {
    Q_GADGET
    };@

    that code compiles with compiler error "staticMetaObject" is not an element of A

    but wenn i change the code to
    @class A
    {

    };

    class helper
    {
    Q_GADGET
    };

    class B: public helper, public A
    {
    Q_GADGET
    };@

    it compiles with no error. I dont really understand why. Is it important for the moc to have the Q_GADGET class before the other inheritance? Are there other solutions instead of just adding an empty dummy class?

    thanks

    Felix



  • I think that you need Q_GADGET in the base class A is all. I can't recall exactly why but it may be because that is where moc tries to store the static meta object related stuff.



  • yah but i cant put it in A, because of the rest of our software. A is a very fundamental class there and i dont want to change it



  • OK, then your second option is to do what you suggested above. It is just like when you make a subclass also inherit form QObject (except that in this case QObject already has the Q_OBJECT macro of course). I don't know of any way around this.



  • Q_OBJECT works only for QObject derived classes. This makes QObject the toplevel class in the inheritance hierarchy. As Q_GADGET enables a subset of the meta object system on classes not based on QObject and moc is run anayways, I suspect this holds for those classes too: moc just expects that the base classes are gadgets too.

    Adding the Q_GADGET macro to class A should have no impact on your existing software, as it just adds some extra stuff and does not change your existing code. The only drawback might be a binary incompatibility, due to the fact that the class interface changes.


Log in to reply