Inheritance in Qt



  • Hi devs!

    This is what I'm trying to do: the class A inherits from B. This the code:

    @// class B.h

    public:
    explicit B();

    // class B.cpp

    B::B() {...}

    //class A.h

    class A : public B
    {
    Q_OBJECT
    public:
    explicit A(QObject *parent = 0);

    //class A.cpp

    A::A(QObject *parent) : B(parent)@

    This doesn't work. Please help me!


  • Moderators

    Since B is not a QObject, and does not have a constructor that accepts QObject argument (see the last line of your code snippet), it will not work. You can just skip the B(parent) call and it should be ok.


  • Moderators

    Also, don't use the Q_OBJECT macro if your class doesn't inherit QObject.


Log in to reply
 

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