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. [SOLVED] [Class Inheritance] Need tips on creating a hierarchic class structure
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] [Class Inheritance] Need tips on creating a hierarchic class structure

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 6.4k 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.
  • V Offline
    V Offline
    valandil211
    wrote on 22 Jun 2011, 14:03 last edited by
    #1

    Hi Qt Devs!

    I know this is a really vague question, but this could become a major problem if I don't ask this.

    For my interface, I want to subclass QWidget to create specialized widgets. I want a widget for each item that I have in my DB (it sounds like a lot, but for my situation, I think this is the best solution). My items are organized in a tree-like structure, each category having similar properties.

    So, I want to create an abstract class that contains very generic virtual member functions that apply to all tests. In turn, I want to have abstract, but further specialized, classes for all nodes in tree structure, down to the very bottom of the tree, where I want to have actual widgets that I can create instances of.

    However, when I tried to do that with one class,

    @#include "cqp.h"

    CQp::CQp(QWidget *parent) :
    QWidget(parent)
    {
    }
    @

    @#ifndef CQP_H
    #define CQP_H

    #include <QWidget>

    #include "testroot.h"

    class CQp : public testroot
    {
    Q_OBJECT
    public:
    explicit CQp(QWidget *parent = 0);

    signals:

    public slots:

    };

    #endif // CQP_H
    @

    and when I try to compile, it complains that 'QWidget' is not a direct base of CQp. True. But I if make it a parent class to CQp explicitly, it complains that class CQp inherits QWidget from two places. Is there a way around that?

    I guess I could make my non-abstract classes inherit QWidget separately, but that seems like ad-hoc solution. Am I missing something?

    Anyhow, sorry for the vague question and long post, and thanks in advance for your time and dedication!

    Joey Dumont

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on 22 Jun 2011, 14:10 last edited by
      #2

      I guess your testroot inherits QWidget? In that case it has to have a similar constructor to your CQp class and you have to call that constructor.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        valandil211
        wrote on 22 Jun 2011, 14:12 last edited by
        #3

        You guessed right, but I don't understand what you mean.

        Joey Dumont

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on 22 Jun 2011, 14:20 last edited by
          #4

          @
          class testroot : public QWidget
          {
          Q_OBJECT
          public:
          testroot(QWidget *parent = 0) : QWidget(parent)
          {}
          }

          CQp::CQp(QWidget *parent) :
          testroot(parent)
          {
          }
          @

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valandil211
            wrote on 22 Jun 2011, 14:30 last edited by
            #5

            Thanks!

            Do you have any good reference book that talks about this kind of inheritance? My code now compiles, but I haven't the slightest idea why. I read about inheritance before, but it's always demonstrated in simple cases, and rarely with abstract classes.

            Thanks, @loladiro!

            Joey Dumont

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on 22 Jun 2011, 14:34 last edited by
              #6

              [quote author="Joey Dumont" date="1308751406"]
              and when I try to compile, it complains that 'QWidget' is not a direct base of CQp. True. But I if make it a parent class to CQp explicitly, it complains that class CQp inherits QWidget from two places. Is there a way around that?[/quote]

              What the compiler is telling you is that from a constructor you can call only direct base constructors.

              That is, if you have an inheritance chain like this
              @
              A --> B --> C
              @
              (C inherits from B, B from A) then from C::C() you can call B::B(), but not A::A(), because it's not a direct base for C.

              The offending line is obviously the call to the QWidget ctor here, since "testcase" is a direct base for CQp, and QWidget is a non-direct base:

              [quote author="Joey Dumont" date="1308751406"]
              @#include "cqp.h"

              CQp::CQp(QWidget *parent) :
              QWidget(parent)
              {
              }
              @
              [/quote]

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • V Offline
                V Offline
                valandil211
                wrote on 22 Jun 2011, 14:36 last edited by
                #7

                And by using,

                @ CQp::CQp(QWidget *parent):
                testroot(parent);

                {
                }
                @

                what am I changing to the inheritance chain?

                Joey Dumont

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dangelog
                  wrote on 22 Jun 2011, 14:44 last edited by
                  #8

                  You're not changing the inheritance graph (which is defined by the class definition); that's a call to a base constructor from your constructor. You're now calling a costructor of a direct-base (allowed) instead of a non-direct base (forbidden).

                  Software Engineer
                  KDAB (UK) Ltd., a KDAB Group company

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    loladiro
                    wrote on 22 Jun 2011, 14:44 last edited by
                    #9

                    Nothing, but you are calling the constructor of your base class (which is the only constructor your are allowed to call in C++).

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      valandil211
                      wrote on 22 Jun 2011, 14:46 last edited by
                      #10

                      Thanks for both your simultaneous answers!

                      Now, will I be able to instantiate the tests that are at the far end of the inheritance graph by using QWidget signals like show() and such? My guess is no, but if so my abstract classes organization scheme falls apart.

                      Any thoughts?

                      Joey Dumont

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        loladiro
                        wrote on 22 Jun 2011, 14:48 last edited by
                        #11

                        Yes you will! Signals/Slots are inherited just like methods (because they are methods).

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          valandil211
                          wrote on 22 Jun 2011, 14:49 last edited by
                          #12

                          Oh, I just got it. Thanks for your patience!

                          Joey Dumont

                          1 Reply Last reply
                          0

                          1/12

                          22 Jun 2011, 14:03

                          • Login

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