Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QAbstractTableModel construction



  • Hello I have a doubt with this class,
    when I create the following class:
    header file:

    #ifndef MYMODEL_H
    #define MYMODEL_H
    
    #include <QAbstractTableModel>
    class MyModel : public QAbstractTableModel
    {
        Q_OBJECT
    public:
        MyModel(QObject *parent);
        int rowCount(const QModelIndex &parent = QModelIndex())const override;
        int columnCount(const QModelIndex &parent = QModelIndex())const override;
        QVariant data(const QModelIndex &index,int role = Qt::DisplayRole) const override;
        QVariant headerData(int section, Qt::Orientation orientation, int role)const;
    
    
    };
    
    #endif // MYMODEL_H
    

    implementation

    #include "mymodel.h"
    
    MyModel::MyModel(QObject *parent):QAbstractTableModel(parent)
    {
    
    }
    int MyModel::rowCount(const QModelIndex &parent) const{
        return 2;
    }
    
    int MyModel::columnCount(const QModelIndex &parent)const{
        return 3;
    }
    
    QVariant MyModel::data(const QModelIndex &index,int role)const{
        if (role == Qt::DisplayRole)
            {
               return QString("Row%1, Column%2")
                           .arg(index.row() + 1)
                           .arg(index.column() +1);
            }
    
    
        return QVariant();
    }
    QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if (role == Qt::DisplayRole)
        {
            if (orientation == Qt::Horizontal) {
                switch (section)
                {
                case 0:
                    return QString("first");
                case 1:
                    return QString("second");
                case 2:
                    return QString("third");
                }
            }
        }
        return QVariant();
    }
    
    

    Main

    #include "mainwindow.h"
    #include <QApplication>
    #include <QTableView>
    #include "mymodel.h"
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTableView tableView;
        MyModel myModel(0);
        tableView.setModel(&myModel);
    
        tableView.show();
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    Why if I dont call methods from mymodel such as QVariant data, does it create the object returned by them? do not should it be included in constructor to be build?

    Thank you!


  • Lifetime Qt Champion

    Hi,

    What object are you talking about ? In your data re-implementation you either return a QString or an empty QVariant.



  • Im talking about QTableView object, build by setting myModel memory address
    why it builds objects returned by methods data, headerdata,rowCount and columnCount if i dont call these methods?


  • Lifetime Qt Champion

    Because tableView calls them as part of its functionality.


Log in to reply