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. Inherit my own class inherited from QObject

Inherit my own class inherited from QObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 3.6k 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.
  • CharlieGC Offline
    CharlieGC Offline
    CharlieG
    wrote on last edited by
    #1

    Hello and Happy New Year to all and to Qt community,

    I Have this class :

    //class1.h
    #include <QObject>
    #include <QDebug>
    #include <QPainter>
    
    class Class1 : public QObject
    {
        Q_OBJECT
    public:
        explicit Class1(QPainter *painter=Q_NULLPTR);
    
    private:
        QPaintDevice *m_device;
        QPainter *m_painter;
    };
    
    //class1.cpp
    #include "class1.h"
    
    Class1::Class1(QPainter *painter)
    {
        m_painter = painter;
        m_device = m_painter->device();
    }
    

    This class correctly run.
    Now, I want create a second class which inherit from Class1. So, I try this solution :

    //class2.h
    #include <QObject>
    #include <QPainter>
    #include "class1.h"
    
    
    class Class1;
    
    class Class2 : public Class1
    {
    public:
        explicit Class2(QPainter *painter=Q_NULLPTR);
    
    };
    
    //class2.cpp
    #include "class2.h"
    
    Class2::Class2(QPainter *painter)
    {
    
    }
    

    But the programm crash without message (only "The program stopped suddenly").

    I have too try without explicit Class2(QPainter *painter=Q_NULLPTR); and without cpp file, but the issue is same.

    Can you help me ?

    Thank you very much in advance.

    Charlie

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      The problem is that Class1 does not work with the default constructor...

      if painter is null then this line: m_device = m_painter->device(); crashes your program.

      Class2 just implicitly calls the default constructor for the base class

      P.S.

      • your classes do not provide the usual QObject* parent in the constructor which is almost standard for QObjects
      • in Class2 you probably want to include the Q_OBJECT macro

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • BjornWB Offline
        BjornWB Offline
        BjornW
        wrote on last edited by
        #3

        This is one reason why I strongly dislike default arguments. Without the

        explicit Class1(QPainter *painter=Q_NULLPTR)
        

        this would have been caught by the compiler; "Class1 does not have a default constructor"

        VRoninV 1 Reply Last reply
        0
        • BjornWB BjornW

          This is one reason why I strongly dislike default arguments. Without the

          explicit Class1(QPainter *painter=Q_NULLPTR)
          

          this would have been caught by the compiler; "Class1 does not have a default constructor"

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @BjornW said in Inherit my own class inherited from QObject:

          This is one reason why I strongly dislike default arguments

          imho this is not a problem with default arguments but just a wrong implementation of the constructor. This would crash even with Class1 *val=new Class1(nullptr); which is not using the default arguments at all

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • BjornWB Offline
            BjornWB Offline
            BjornW
            wrote on last edited by BjornW
            #5

            @VRonin
            Well, he would not have been able to do it wrong if it weren't for the default argument. The compiler would have caught it.

            EDIT: To clarify, he would not have been able to implement the Class2 constructor in this way if it weren't for the default argument in Class1 constructor.

            I've had problems with default arugments before, for example, let's say I have this class:

            class MyClass
            {
               public:
               MyClass(MyClass* pParent = 0); 
               ...
            }
            
            ...
            
            //Somewhere deep in my program...
            auto p1 = new MyClass(0);      //1 has no parent
            auto p2 = new MyClass(p1);     //2 has parent: 1
            

            Now I make changes to my class, like so:

            class MyClass
            {
               public:
               MyClass(MyClass* pBuddy, MyClass* pParent = 0);
            }
            
            ...
            
            //Somewhere deep in my program...
            auto p1 = new MyClass(0);     //1 has no buddy and no parent
            auto p2 = new MyClass(p1);    //2 has buddy: 1 and no parent
            

            Suddenly, my program does not work because the parent-child relationship broke. The compiler would have caught this and prompted me to add the correct arguments, if it wasn't for the default argument.

            Anyway, I digress. Careful with your constructors! :-D

            1 Reply Last reply
            1
            • N Offline
              N Offline
              NameRakes
              wrote on last edited by
              #6

              @BjornW, I am new to Qt, and am still getting used to the nullptr (sorry no 0) default-value ctors. I attempt to do exactly like you - in my classes that derive from QClasses have no default args. In fact in my pure C++ classes, I leave no default ctor around. So I definitely have all the 5 ctors declared, of course some with the new = delete syntax.

              I thought they (Qt-architects) must have had a good reason for this nullptr-default-value-idiom, but nevertheless I follow my own strict rules. Hopefully these different idioms play well together :-)

              1 Reply Last reply
              0

              • Login

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