Models



  • 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



  • @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?



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


  • Lifetime Qt Champion

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.