I didn't understand the Qt conception. You have to set data in setData()
(store check state) and then populate model in data()
with this new value being returned for checkbox, alike this:
setData()
bool TreeModel::setData(const QModelIndex & index, const QVariant & value, int role) {
if (role == Qt::CheckStateRole && index.column() == 1) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
QTreeWidgetItem *check = static_cast<QTreeWidgetItem*>(index.internalPointer());
if (value == Qt::Checked) {
checkedState_ = Qt::Checked;
}
else {
checkedState_ = Qt::Unchecked;
}
}
emit dataChanged(index, index);
return true;
}
data()
QVariant TreeModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (role == Qt::CheckStateRole) {
if (index.column() == 1) {
return checkedState_;
}
}
else if (role == Qt::DisplayRole) {
if (index.column() != 1) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
}
return QVariant();
}