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. Models

Models

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 553 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by
    #1

    Hello, I am learning QModels customizing and I come up with a doubt, when I create a class (for instance Mymodel) and create constructor method (why have I to pass *parent pointer). And then when in main class I pass MyModel to a tableview it is as if all MyModel methods were executed but I don't actually call them ,why are methods executed??

    Thank you

    JonBJ KillerSmathK 2 Replies Last reply
    0
    • J jss193

      Hello, I am learning QModels customizing and I come up with a doubt, when I create a class (for instance Mymodel) and create constructor method (why have I to pass *parent pointer). And then when in main class I pass MyModel to a tableview it is as if all MyModel methods were executed but I don't actually call them ,why are methods executed??

      Thank you

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

      @jss193
      Once you have called something like QAbstractItemView::setModel(QAbstractItemModel *model) to bind the view to your model class, the view will call methods of your model (e.g. data()) to access the data to display. Is that what you mean?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #3

        you mean that it automatically calls all public methods, true?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          The view will call whatever method it needs to show the content of your model.

          As for your question about the parent parameter, it's not mandatory unless when explicitly required. The parameter is used to put your QObject based classed in the object tree of the parent you pass to it.

          For more information, see the Object Trees & Ownership chapter in Qt's documentation.

          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
          2
          • J jss193

            Hello, I am learning QModels customizing and I come up with a doubt, when I create a class (for instance Mymodel) and create constructor method (why have I to pass *parent pointer). And then when in main class I pass MyModel to a tableview it is as if all MyModel methods were executed but I don't actually call them ,why are methods executed??

            Thank you

            KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by KillerSmath
            #5

            Hi @jss193 and welcome to Qt Forum.

            when I create a class (for instance Mymodel) and create constructor method (why have I to pass *parent pointer)

            I suggest you to read about the ownership relation between Objects inherited classes.

            https://doc.qt.io/qt-5/objecttrees.html

            In short, QModels classes inheret (in top level) a QObject class. When you pass a parent pointer, it will add the actual object as child of parent pointer, it means, if the parent pointer is deleted in some part of your code then it will delele all its children. (That's behavior is similiar of a garbage collection of top level class).

            Below is an example of this feature

            #include <QObject>
            // class definitons
            class A : public QObject{
                Q_OBJECT
            public:
                A(QObject *parent = nullptr) : QObject(parent) { }
            };
            
            class B : public QObject {
                Q_OBJECT
            public:
                B(QObject *parent = nullptr) : QObject(parent) {}
            };
            
            // implementations
            
            A *a = new A;
            B *b = new B(a);
            
            QObject::connect(a, &A::destroyed, [](){
                qDebug() << "A object has been destroyed";
            });
            
            QObject::connect(b, &B::destroyed, [](){
                qDebug() << "B object has been destroyed";
            });
            
            deleta a;
            

            I pass MyModel to a tableview it is as if all MyModel methods were executed but I don't actually call them ,why are methods executed??

            Look at setModel function signature from QTableView class.
            virtual void setModel(QAbstractItemModel *model) override

            You are passing a Derived class pointer as parameter to setModel function.

            This is a feature of O.O paradigm where you can pass a Derived class with function redefinitions and execute it with Base Pointer.

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            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