[Solved] Subclassing from QObject base and non Qobject base classes



  • Hi Folks,

    I have the following architecture:

    @
    class nativeBase {
    protected: nativeBase();
    ... some stuff...
    }

    class nativeDerived: public nativeBase {
    protected: nativeDerived();
    ... some stuff...
    }
    @

    Please consider the protected constructor due to prevent the anauthorized creation, and the fact, that there is no Qt specific stuff, like signals or slots...

    @
    class qobjectBase: public QObject {
    Q_OBJECT

    ... some Qt signals and slots...
    protected: qobjectBase();
    ... some stuff...
    }
    @

    Lets consider the protected constructor, as above and there are some signals and / or slots.

    @
    class surface: public qobjectBase, public nativeDerived {
    Q_OBJECT
    }
    @

    During compilation, I get an error, that there are no qt_metatype(), and other methods which are included by the Q_OBJECT macro whitin the nativeDerived class. It is true, because I don't introduce it. I investigated that I derived the nativeBase from QObject, but in this case, I get an error, that multiple inheritance from QObject is prohibited.

    The question is: how can I solve this issue? Maybe, is any solution to tell to Qt, that this is not needed to iterate over the "native" inheritance chain?

    Regards,
    Norbert



  • Can you provide more details? This test based on your example builds and runs for me:

    @#include <QObject>
    #include <cstdio>

    class nativeBase {
    protected: nativeBase(){}
    };

    class nativeDerived: public nativeBase {
    protected: nativeDerived(){}
    };

    class qobjectBase: public QObject {
    Q_OBJECT

    protected: qobjectBase(){}
    };

    class surface: public qobjectBase, public nativeDerived {
    Q_OBJECT
    signals:
    void sig();
    public slots:
    void slot(){ printf("slot!\n"); }
    };

    int main(int argc, char *argv[])
    {
    Q_UNUSED(argc);
    Q_UNUSED(argv);
    surface s;
    QObject::connect(&s,&surface::sig, &s, &surface::slot);
    emit s.sig();
    return 0;
    }

    #include "main.moc"@

    Regarding deriving a class from multiple classes that inherit QObject, that's the diamond inheritance problem. http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem


  • Lifetime Qt Champion

    Hi,

    The diamond problem is not really applicable here since you can't inherit multiple times from QObject.



  • [quote author="jeremy_k" date="1409428351"]Can you provide more details? This test based on your example builds and runs for me:

    @#include <QObject>
    #include <cstdio>

    class nativeBase {
    protected: nativeBase(){}
    };

    class nativeDerived: public nativeBase {
    protected: nativeDerived(){}
    };

    class qobjectBase: public QObject {
    Q_OBJECT

    protected: qobjectBase(){}
    };

    class surface: public qobjectBase, public nativeDerived {
    Q_OBJECT
    signals:
    void sig();
    public slots:
    void slot(){ printf("slot!\n"); }
    };

    int main(int argc, char *argv[])
    {
    Q_UNUSED(argc);
    Q_UNUSED(argv);
    surface s;
    QObject::connect(&s,&surface::sig, &s, &surface::slot);
    emit s.sig();
    return 0;
    }

    #include "main.moc"@

    Regarding deriving a class from multiple classes that inherit QObject, that's the diamond inheritance problem. http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem[/quote]

    Hi,

    I think, you are a bit confused: there are no signals and / or slots whitin the surface class. The signals and / or slots are only defined and implemented in the qobjectBase class, but in order to use their in connect() method, I use those via the surface class.



  • [quote author="SGaist" date="1409433572"]Hi,

    The diamond problem is not really applicable here since you can't inherit multiple times from QObject.[/quote]

    Hi,

    yes, you are right, it is not an exactly diamond problem



  • [quote author="moravas" date="1409482210"]
    Hi,

    I think, you are a bit confused: there are no signals and / or slots whitin the surface class. The signals and / or slots are only defined and implemented in the qobjectBase class, but in order to use their in connect() method, I use those via the surface class.[/quote]

    Great. So post an example that demonstrates your problem.



  • Hi,

    this is the concrete architecture:
    @
    class nativeBase {
    public: virtual ~nativeBase();

    protected: nativeBase();

    protected: QSettings _cfg;
    };

    class nativeDerived: public nativeBase {

    public: virtual ~nativeDerived();

    protected: nativeDerived();

    private: QString _processName;
    };

    class qobjectBase: public QObject {
    Q_OBJECT

    public: virtual ~qobjectBase();

    signals: void sigChanged();

    public slots: void onNew(const QString& name);
    public slots: void onOpen(const QString& name);
    public slots: void onDelete(const QString& name);
    public slots: void onSaveAs(const QString& name);

    protected: qobjectBase();
    };

    class surface: public nativeDerived, public qobjectBase {
    Q_OBJECT
    };
    @

    Regards,
    Norbert



  • Moc assumes that the QObject derived class comes first in the order of inheritance.

    http://qt-project.org/doc/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first



  • Hi,

    thanks a lot!!! It seems to be helpful.

    Bye


Log in to reply
 

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