I have a general question about the implementation of the underlying data source of a QAbstractTableModel
.
The QAbstractTableModel::data()
function access the data sources content by an index consisting of a row and a column value.
If my underlying data source is a QList
of Person
classes, where each member represents a column, how do I access its members with a given column index?
The only approach I can think of is that I use a kind of mapping, that maps a column number to a member of the Person
class:
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
Person person = mySource[index.row()];
if (index.column() == 0)
return person.getName();
else if (index.column() == 1)
return person.getAdress();
(...and so on..)
}
return QVariant();
}
Is this the approach to tackle this problem or is there a better one? If my class has 50 members that would be a lot of work to do. The same thing must be done, when I write data to the source via QAbstractTableModel::setData()
.
Answers or links to material that would help me to understand this part of the model/view implementation in Qt are very much appriciated.