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. QAbstractTableModel construction
Forum Update on Monday, May 27th 2025

QAbstractTableModel construction

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

    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!

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

      Hi,

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

      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
      0
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #3

        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?

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

          Because tableView calls them as part of its functionality.

          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
          0

          • Login

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