I have a model
class TreeModel : public QAbstractItemModel
which I populate with instances of my TreeItem excluding column == 1 . In column 1, I created CheckBoxes :
QVariant TreeModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole) { if (role == Qt::CheckStateRole) { if (index.column() == 1) { if (index.row() == 1) { return Qt::Unchecked; } else return Qt::Checked; } } return QVariant(); } if (role == Qt::DisplayRole) { if (index.column() != 1) { TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); return item->data(index.column()); } } return QVariant(); }
I can set these CheckBoxes statuses in Qt::Checked or Qt::Unchecked , but my problem is: I cannot change them later when they are clicked (however setData is called with the corresponding index.column==1 and role==Qt::CheckStateRole ). I saw examples with ItemDelegate - only this seems to work. It's true? Should I use a delegate in this scenario?
Here is my setData() function:
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());
4pie0 source share