Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Inheritance with QObject (parent?)
Forum Updated to NodeBB v4.3 + New Features

Inheritance with QObject (parent?)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    Hello,

    I would like to know what it necessary to proper inherit from QObject.
    I have two example classes. Class A which is my base class derived from QObject and Class B which is derived from Class A.

    I already found out that I am only allowed to inherit from QObject with the first class. But how do I need to modify the constructors that the parent system is working?

    Here is a minimal not working example which tells me that the constructor implementation of B has QObject not as an element and not as Base.

    What do I need to do?

    Thank you very much!

    a.h:

    #ifndef A_H
    #define A_H
    
    #include <QObject>
    
    class A : public QObject
    {
        Q_OBJECT
    public:
        explicit A(QObject *parent = nullptr);
    };
    
    #endif // A_H
    

    a.cpp:

    #include "a.h"
    
    A::A(QObject *parent) : QObject(parent)
    {
    }
    

    b.h:

    #ifndef B_H
    #define B_H
    
    #include <QObject>
    #include "a.h"
    
    class B : protected A
    {
        Q_OBJECT
    public:
        explicit B(QObject *parent = nullptr);
    };
    
    #endif // B_H
    

    b.cpp:

    #include "b.h"
    
    B::B(QObject *parent): QObject(parent)
    {
    }
    

    main:

    #include <QCoreApplication>
    #include <b.h>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        B myclass;
    
        return a.exec();
    }
    
    SGaistS 1 Reply Last reply
    0
    • R robro

      Hello,

      I would like to know what it necessary to proper inherit from QObject.
      I have two example classes. Class A which is my base class derived from QObject and Class B which is derived from Class A.

      I already found out that I am only allowed to inherit from QObject with the first class. But how do I need to modify the constructors that the parent system is working?

      Here is a minimal not working example which tells me that the constructor implementation of B has QObject not as an element and not as Base.

      What do I need to do?

      Thank you very much!

      a.h:

      #ifndef A_H
      #define A_H
      
      #include <QObject>
      
      class A : public QObject
      {
          Q_OBJECT
      public:
          explicit A(QObject *parent = nullptr);
      };
      
      #endif // A_H
      

      a.cpp:

      #include "a.h"
      
      A::A(QObject *parent) : QObject(parent)
      {
      }
      

      b.h:

      #ifndef B_H
      #define B_H
      
      #include <QObject>
      #include "a.h"
      
      class B : protected A
      {
          Q_OBJECT
      public:
          explicit B(QObject *parent = nullptr);
      };
      
      #endif // B_H
      

      b.cpp:

      #include "b.h"
      
      B::B(QObject *parent): QObject(parent)
      {
      }
      

      main:

      #include <QCoreApplication>
      #include <b.h>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          B myclass;
      
          return a.exec();
      }
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @robro said in Inheritance with QObject (parent?):

      B::B(QObject *parent): QObject(parent)

      This is wrong, your B object is of A type, therefore:

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • R Offline
        R Offline
        robro
        wrote on last edited by robro
        #3

        Thank you very much :-)

        It worked, but now I got another error.
        In reality I use the class B togehter with QML.

        So I use the following lines:

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        #include "b.h"
        
        int main(int argc, char *argv[])
        {
        
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv);
        
        qmlRegisterType<B>("com.br.classes", 1, 0, "b");
        
        B b
        QQmlApplicationEngine engine;
            engine.rootContext()->setContextProperty("b", &b); //Throws the error
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        return app.exec();
        

        This leads to the following error: "Conversion from B* to QObject* is already existing but it cannot be accesed"

        How can I solve that?

        J.HilkJ 1 Reply Last reply
        0
        • R robro

          Thank you very much :-)

          It worked, but now I got another error.
          In reality I use the class B togehter with QML.

          So I use the following lines:

          #include <QGuiApplication>
          #include <QQmlApplicationEngine>
          #include <QQmlContext>
          #include "b.h"
          
          int main(int argc, char *argv[])
          {
          
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv);
          
          qmlRegisterType<B>("com.br.classes", 1, 0, "b");
          
          B b
          QQmlApplicationEngine engine;
              engine.rootContext()->setContextProperty("b", &b); //Throws the error
          engine.load(QUrl(QLatin1String("qrc:/main.qml")));
          return app.exec();
          

          This leads to the following error: "Conversion from B* to QObject* is already existing but it cannot be accesed"

          How can I solve that?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @robro said in Inheritance with QObject (parent?):

          This leads to the following error: "Conversion from B* to QObject* is already existing but it cannot be accesed"
          How can I solve that?

          you declared the base class protected:

          class B : protected A

          And therefore can't be accessed from outside the class, I would change that.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          5

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved