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. Inheriting from a QAbstractTableModel based class?

Inheriting from a QAbstractTableModel based class?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 668 Views
  • 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.
  • A Offline
    A Offline
    andi456
    wrote on last edited by andi456
    #1

    Hi,

    would it be a good idea to inherit from a QAbstractTableModel based class? I tried it, but ran into some obscure syntax issues.
    I put this in the .h file, but could not get the .cpp file to work, apparently because the parent's class has a parameter in the constructor other than "parent".

    class myChildTableModel : public myParentTableModel
    {
    Q_OBJECT
    public:
    explicit myChildTableModel(QObject *parent = nullptr);

    };

    Anyhow, I'd like to know if it is worth the headache to get it to work, or should I just inherit from QAbstractTableModel, although it would be nice to save some typing in the future...?

    Kind regards,

    Andreas

    Edit: "public" to the code that i've forgotten to type in before.

    JonBJ 1 Reply Last reply
    0
    • A andi456

      Hi,

      would it be a good idea to inherit from a QAbstractTableModel based class? I tried it, but ran into some obscure syntax issues.
      I put this in the .h file, but could not get the .cpp file to work, apparently because the parent's class has a parameter in the constructor other than "parent".

      class myChildTableModel : public myParentTableModel
      {
      Q_OBJECT
      public:
      explicit myChildTableModel(QObject *parent = nullptr);

      };

      Anyhow, I'd like to know if it is worth the headache to get it to work, or should I just inherit from QAbstractTableModel, although it would be nice to save some typing in the future...?

      Kind regards,

      Andreas

      Edit: "public" to the code that i've forgotten to type in before.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @andi456
      I don't know what exactly your issue with your code is. You already seem to have done it for your myParentTableModel, so what's the problem with myChildTableModel? Depending on your issue, you might want

      class myChildTableModel : public myParentTableModel
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        andi456
        wrote on last edited by andi456
        #3

        Well, the compiler tells me that there's something wrong with the constructor of the parent class, which has one parameter more than just "parent" and I haven't figured out so far, what i am doing wrong:

        myChildTableModel::myChildTableModel(QObject *parent): myParentTableModel(parent) {
        }

        doesn't work, because the number of arguments passed to the parent's class constructor is wrong - understandable. But if I put the parameter with exact type in there, the error changes to:
        "Expected '(' for function-style cast or type construction"

        JonBJ 1 Reply Last reply
        0
        • A andi456

          Well, the compiler tells me that there's something wrong with the constructor of the parent class, which has one parameter more than just "parent" and I haven't figured out so far, what i am doing wrong:

          myChildTableModel::myChildTableModel(QObject *parent): myParentTableModel(parent) {
          }

          doesn't work, because the number of arguments passed to the parent's class constructor is wrong - understandable. But if I put the parameter with exact type in there, the error changes to:
          "Expected '(' for function-style cast or type construction"

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @andi456

          doesn't work, because the number of arguments passed to the parent's class constructor is wrong - understandable

          Since you don't show whatever the declaration of your myParentTableModel is it's hard to say.

          But if I put the parameter with exact type in there, the error changes to:

          "Expected '(' for function-style cast or type construction"

          Since you don't show what you typed to cause the error it's hard to say. If you mean you wrote something like

          myChildTableModel::myChildTableModel(QObject *parent): myParentTableModel(SomeClass *parent)
          

          that would not be legal C++.

          1 Reply Last reply
          2
          • A Offline
            A Offline
            andi456
            wrote on last edited by
            #5

            It's not too complicated, just a json parameter, so in the .h it looks like this:
            explicit myParentTableModel(nlohmann::json j_list, QObject *parent = nullptr);

            and the .h file of the child looks like this:

            explicit myChildTableModel(QObject *parent = nullptr);

            I must admit that i'm not sure, if parent parameter is in order though.

            JonBJ Pl45m4P 2 Replies Last reply
            0
            • A andi456

              It's not too complicated, just a json parameter, so in the .h it looks like this:
              explicit myParentTableModel(nlohmann::json j_list, QObject *parent = nullptr);

              and the .h file of the child looks like this:

              explicit myChildTableModel(QObject *parent = nullptr);

              I must admit that i'm not sure, if parent parameter is in order though.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @andi456
              So your parent class's constructor takes 2 parameters, the first being mandatory and the second being optional, and you declare a subclass whose constructor takes only the optional second parameter and from that call the base class with just the one (optional) parameter passed. And then you wonder why that doesn't compile? Your myParentTableModel() constructor requires a nlohmann::json parameter in first position, including if called from myChildTableModel() constructor....

              1 Reply Last reply
              3
              • A andi456

                It's not too complicated, just a json parameter, so in the .h it looks like this:
                explicit myParentTableModel(nlohmann::json j_list, QObject *parent = nullptr);

                and the .h file of the child looks like this:

                explicit myChildTableModel(QObject *parent = nullptr);

                I must admit that i'm not sure, if parent parameter is in order though.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @andi456

                As @JonB pointed out, you need at least two constructors to make it work.

                // default c'tor where myParentTableModel() is valid
                explicit myParentTableModel(QObject *parent = nullptr);
                // your enhanced, second c'tor, which requires a json arg
                explicit myParentTableModel(nlohmann::json j_list, QObject *parent = nullptr);
                

                However, if your myChildTableModel can not serve the required args of its base class (
                myParentTableModel(nlohmann::json, QObject *)) the second c'tor, is never used, unless you maybe implement another child class which also accepts a nlohmann::json and therefore can use it to call your overloaded c'tor.

                class anotherTableModel : public myParentTableModel
                {
                     Q_OBJECT
                
                  public:
                     
                     explicit anotherTableModel (nlohmann::json j, QObject *parent = nullptr);
                
                };
                
                

                Or you add this to your existing class:

                class myChildTableModel : public myParentTableModel
                {
                     Q_OBJECT
                
                  public:
                     
                     explicit myChildTableModel(QObject *parent = nullptr);
                     // matches parentTable = base class c'tor
                     explicit myChildTableModel(nlohmann::json j, QObject *parent = nullptr);
                
                };
                

                and implement it like:

                myChildTableModel::myChildTableModel(nlohmann::json j, QObject *parent)
                      : myParentTableModel(j, parent){}
                

                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  andi456
                  wrote on last edited by andi456
                  #8

                  No, i actually do not wonder, that it doesn't compile, because I do not know how to handle "QObject parent" stuff correctly.

                  In the .cpp of I got myParentTableModel::myParentTableModel(nlohmann::json j_list, QObject *parent)
                  : QAbstractTableModel(parent) {
                  initialization code
                  }
                  (The class works as expected in a QTableView.)
                  But the analogon for the child class is not clear to me. Do I need a parent parameter of the type myParentTableModel or of the type QObject?

                  Pl45m4P JonBJ 2 Replies Last reply
                  0
                  • A andi456

                    No, i actually do not wonder, that it doesn't compile, because I do not know how to handle "QObject parent" stuff correctly.

                    In the .cpp of I got myParentTableModel::myParentTableModel(nlohmann::json j_list, QObject *parent)
                    : QAbstractTableModel(parent) {
                    initialization code
                    }
                    (The class works as expected in a QTableView.)
                    But the analogon for the child class is not clear to me. Do I need a parent parameter of the type myParentTableModel or of the type QObject?

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @andi456 said in Inheriting from a QAbstractTableModel based class?:

                    But the analogon for the child class is not clear to me. Do I need a parent parameter of the type myParentTableModel or of the type QObject?

                    Check my comment above.

                    It propagates.

                    • grandchildClass (int i, int x, QObject *parent) : child(x, parent) // takes i, x, and potential parent
                      • childClass (int x, QObject *parent) : parentClass(parent) // takes x, and potential parent
                        • parentClass (QObject *parent) : QObject(parent) // takes potential parent and is directly derived from QObject base class

                    Called top-down, where next "generation" is more specific.
                    You only invoke the matching constructor of the direct base class (doesn't matter how many times you've subclassed already).
                    All four are called, when calling something like:

                    QMainWindow mw;
                    grandchildClass *grandchild = new grandchildClass(42, 1337, &mw);
                    

                    Btw: when grandchild's QObject *parent is nullptr of course it does not mean that it has no "parent" (base- / "super"-) class... it is dependent on the classes where you derive from. QObject *parent = nullptr means that grandchildClass object is a standalone / toplevel object in QObject / Qt "terms". For example, then there is no other QObject which cleans it up when destroyed.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    2
                    • A andi456

                      No, i actually do not wonder, that it doesn't compile, because I do not know how to handle "QObject parent" stuff correctly.

                      In the .cpp of I got myParentTableModel::myParentTableModel(nlohmann::json j_list, QObject *parent)
                      : QAbstractTableModel(parent) {
                      initialization code
                      }
                      (The class works as expected in a QTableView.)
                      But the analogon for the child class is not clear to me. Do I need a parent parameter of the type myParentTableModel or of the type QObject?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @andi456
                      Most Qt classes (QObject/QWidget-derived) take an optional QObject *parent argument. This has nothing to do with myParentTableModel. I imagine the use of Parent in that name is confusing, it's not clear how it's a "parent" if you have class myChildTableModel : public myParentTableModel. "Parentage" in terms of class-derivation is quite different from what SomeClassConstructor(QObject *parent = nullptr); is all about. The latter requires some other object (which itself is derived from QObject) to act as its parent at runtime (nothing to do with classes/compile time). That is used by Qt to create a hierarchy of QObjects. When you destroy a QObject it destroys all its child QObhects for you. This is a very useful convenience to save you having to code carefully to achieve that.

                      Please read Object Trees & Ownership. You need to understand this for Qt programming.

                      Just as an example. Say you have a QMainWindow for your top-level widget. And say you only create your QAbstractModel in that window's code (it's not used elsewhere). You want the model pointed to by your QAbstractModel *model member variable in your main window to get destroyed when the main window gets destroyed. If you don't that your code will leak memory. You could override the main window's destructor and destroy your model there explicitly yourself. Or from within your code you could construct the model via model = new QAbstractModel(theMainWindowInstance);/ (theMainWindowInstance would be this if you have derived from QMainWindow and write this code inside your derived class's code.) The main window instance would be the parent of the model, and whenever the main window is destroyed by whatever means it would destroy the model for you.

                      1 Reply Last reply
                      1
                      • A Offline
                        A Offline
                        andi456
                        wrote on last edited by
                        #11

                        Thanks for your help. Got it working now.

                        Kind regards

                        1 Reply Last reply
                        0
                        • A andi456 has marked this topic as solved on
                        • GrecKoG Offline
                          GrecKoG Offline
                          GrecKo
                          Qt Champions 2018
                          wrote on last edited by
                          #12

                          I would question the need to inherit from a concrete model subclass. Are you sure a proxy model is not more appropriate?

                          1 Reply Last reply
                          2

                          • Login

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