Model is an interface to actual data, which are stored in some underlaying data structure. So if I would like to have a list of checkable rows in my QListView then my QAbstractItemModel subclass will contain some private member like:
QList<QPair<bool, QString> > m_modelData; // pair (checked?, name)
QList<QPair<bool, QString> > m_modelData; // pair (checked?, name)
To copy to clipboard, switch view to plain text mode
And in data() method it will look like this:
// . . . some checks for other roles and index validity
if (role == Qt::CheckStateRole) {
return m_modelData.at(index.row()).first ? Qt::Checked : Qt::Unchecked;
}
// . . .
// . . . some checks for other roles and index validity
if (role == Qt::CheckStateRole) {
return m_modelData.at(index.row()).first ? Qt::Checked : Qt::Unchecked;
}
// . . .
To copy to clipboard, switch view to plain text mode
I think that you assumed that this check state would be stored somewhere by the model itself, but it is you, who is supposed to write the actual data structure where this check state would be stored.
And don't forget about setData() where you have to update data in your structure while it is called with Qt::CheckStateRole.
Bookmarks