
Originally Posted by
been_1990
I already can change the checkbox state from checked to unchecked. But that still checks all of them(like you said it would..)
Do you think the code of QStandardItemModel would have the implementation to check/uncheck items independently?
I don’t think the code it has would be very useful, since it relies on instances of QStandardItem to keep the data, and you don’t have a model built on QStandardItems, you have a QFileSystemModel, the backing store of which is not accessible to programs that use the model.
Try something like this (off the top of my head, so errors/typos are not unlikely):
class CustomModel : public QFileSystemModel {
//...
QSet<QPersistentModelIndex> checklist;
//...
};
QVariant CustomModel
::data(const QModelIndex
& index,
int role
) const { if (role == Qt::CheckStateRole) return checklist.contains(index) ? Qt::Checked : Qt::Unchecked;
return QFileSystemModel::data(index, role);
}
Qt::ItemFlags CustomModel::flags(const QModelIndex& index) const {
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
}
bool CustomModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::CheckStateRole) {
if (value == Qt::Checked) checklist.insert(index);
else checklist.remove(index);
emit dataChanged(index, index);
return true;
}
return QFileSystemModel::setData(index, value, role);
}
class CustomModel : public QFileSystemModel {
//...
QSet<QPersistentModelIndex> checklist;
//...
};
QVariant CustomModel::data(const QModelIndex& index, int role) const {
if (role == Qt::CheckStateRole) return checklist.contains(index) ? Qt::Checked : Qt::Unchecked;
return QFileSystemModel::data(index, role);
}
Qt::ItemFlags CustomModel::flags(const QModelIndex& index) const {
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
}
bool CustomModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::CheckStateRole) {
if (value == Qt::Checked) checklist.insert(index);
else checklist.remove(index);
emit dataChanged(index, index);
return true;
}
return QFileSystemModel::setData(index, value, role);
}
To copy to clipboard, switch view to plain text mode
Note that the QSet keeps QPersistentModelIndexes, not ordinary QModelIndexes.
Bookmarks