I have column with a checkbox in a QTableView
. The checkbox is generated by:
- returning
Qt::ItemIsUserCheckable
in overriddenflags
member function - in overridden
data()
function I return aQt::CheckState
forrole == Qt::CheckStateRole
according to data
Works, see screenshot.
But beside the checkbox I have some editable textbox in the column. How can I get rid of this textbox (where I have entered "dsdsdsds" for demonstration? Clarification, the checkbox shall be editable, but nothing else.
As requested, I can only show simplified version
Qt::ItemFlags MyClass::flags(const QModelIndex &index) const {
Qt::ItemFlags f = QAbstractListModel::flags(index);
... return f if index is not target column ....
// for target column with checkbox
return (f | Qt::ItemIsEditable | Qt::ItemIsUserCheckable; )
}
QVariant MyClass::data(const QModelIndex &index, int role) const {
.. do something for other columns
.. for checkbox column
if (role != Qt::CheckStateRole) { return QVariant(); }
bool b = ... get value for checkbox column
Qt::CheckState cs = b ? Qt::Checked : Qt::Unchecked;
return QVariant(static_cast<int>(cs));
}
If I remove Qt::ItemIsEditable
then the checkbox is read only too. I later found an SO answer with a similar approach.
Remark: No duplicate of A checkbox only column in QTableView