How to use QAbstractListModel: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(QAbstractItemModel::rowCount and QAbstractItemModel::columnCount return int, not void.)
m (Fixed and normalised the append calls in the populate function of the UserModel.)
 
(One intermediate revision by one other user not shown)
Line 3: Line 3:
We want to display a user list. First create a simple User class as follow :  
We want to display a user list. First create a simple User class as follow :  


<code>
class User  
class User  
{
{
   public :  
   public :  
     User();
     User();
Line 13: Line 12:
     QString mLastName;
     QString mLastName;
     QDate mBirthday;
     QDate mBirthday;
 
}
}
</code>


We are now going to create a model based on QAbstractItemModel. A model is an interface between the view and the data. In the following example, data are a list of User.  
We are now going to create a model based on QAbstractItemModel. A model is an interface between the view and the data. In the following example, data are a list of User.  


<code>
class UserModel : public QAbstractItemModel  
class UserModel : public QAbstractItemModel  
{
{
   Q_OBJECT
   Q_OBJECT
   public :  
   public :  
Line 29: Line 25:
     QVariant data(const QModelIndex &index, int role) const;
     QVariant data(const QModelIndex &index, int role) const;
     void populate();
     void populate();
   private:
   private:
     QList<User> mDatas;
     QList<User> mDatas;
}


}
int UserModel::rowCount(const QModelIndex& parent = QModelIndex()) const
</code>
 
<code>
    int UserModel::rowCount(const QModelIndex& parent = QModelIndex()) const
     {
     {
       return mDatas.size();
       return mDatas.size();
     }
     }
     int UserModel::columnCount(const QModelIndex& parent = QModelIndex()) const
     int UserModel::columnCount(const QModelIndex& parent = QModelIndex()) const
     {
     {
Line 47: Line 38:
     }
     }


  QVariant UserModel::data(const QModelIndex &index, int role) const
  QVariant UserModel::data(const QModelIndex &index, int role) const
   {
   {
     if (!index.isValid())
     if (!index.isValid())
         return QVariant();
         return QVariant();
     if ( role == Qt::DisplayRole)
     if ( role == Qt::DisplayRole)
     {
     {
         if ( index.column() == 0)
         if ( index.column() == 0)
             return mDatas[index.row()].firstname();
             return mDatas[index.row()].firstname();
         if ( index.column() == 1)
         if ( index.column() == 1)
             return mDatas[index.row()].lastname();
             return mDatas[index.row()].lastname();
       if ( index.column() == 2)
       if ( index.column() == 2)
             return mDatas[index.row()].birthday();
             return mDatas[index.row()].birthday();
     }
     }
     return QVariant();
     return QVariant();
}


}
void UserModel::populate()
 
  void UserModel::populate()
   {
   {
         beginResetModel();
         beginResetModel();
         mDatas.clear();
         mDatas.clear();
         mDatas.append(User("Charles","Darwin", QDate(1812,22,23));
         mDatas.append(User("Charles", "Darwin", QDate(1812,22,23)));
         mDatas.append(User("Lars","Knoll,"QDate(1976,22,12));
         mDatas.append(User("Lars", "Knoll", QDate(1976,22,12)));
         mDatas.append(User("Boby","Lapointe",QDate(1951,21,31));
         mDatas.append(User("Boby", "Lapointe", QDate(1951,21,31)));
         endResetModel();
         endResetModel();
   }
   }
</code>


Now use your mode :  
Now use your mode :  


<code>
QTableView * view = new QTableView;
 
UserModel * model = new UserModel;
QTableView * view = new QTableView;
view->setModel(model);
UserModel * model = new UserModel;
model->populate()
view->setModel(model);
model->populate()
 
</code>

Latest revision as of 22:26, 1 March 2021

The following tutorial explains how to create a custom Qt model and use it inside a QTableView.

We want to display a user list. First create a simple User class as follow :

class User 
{
 public : 
    User();
    User(const QString& firstname, const QString& lastname, const QDate& birthday);   
 private:
    QString mFirstname; 
    QString mLastName;
    QDate mBirthday;
}

We are now going to create a model based on QAbstractItemModel. A model is an interface between the view and the data. In the following example, data are a list of User.

class UserModel : public QAbstractItemModel 
{
  Q_OBJECT
 public : 
    UserModel(QObject * parent = 0);
    int rowCount(const QModelIndex& parent = QModelIndex()) const;
    int columnCount(const QModelIndex& parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    void populate();
 private:
   QList<User> mDatas;
}
int UserModel::rowCount(const QModelIndex& parent = QModelIndex()) const
   {
      return mDatas.size();
   }
   int UserModel::columnCount(const QModelIndex& parent = QModelIndex()) const
   {
      return 3;
   }
 QVariant UserModel::data(const QModelIndex &index, int role) const
  {
   if (!index.isValid())
       return QVariant();
   if ( role == Qt::DisplayRole)
   {
       if ( index.column() == 0)
           return mDatas[index.row()].firstname();
       if ( index.column() == 1)
           return mDatas[index.row()].lastname();
      if ( index.column() == 2)
           return mDatas[index.row()].birthday();
   }
   return QVariant();
}
void UserModel::populate()
  {
        beginResetModel();
        mDatas.clear();
        mDatas.append(User("Charles", "Darwin", QDate(1812,22,23)));
        mDatas.append(User("Lars", "Knoll", QDate(1976,22,12)));
        mDatas.append(User("Boby", "Lapointe", QDate(1951,21,31)));
        endResetModel();
  }

Now use your mode :

QTableView * view = new QTableView;
UserModel * model = new UserModel;
view->setModel(model);
model->populate()