I have column with a checkbox in a QTableView
. The checkbox is generated by:
Qt::ItemIsUserCheckable
in overridden flags
member functiondata()
function I return a Qt::CheckState
for role == Qt::CheckStateRole
according to dataWorks, 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
Replace the flag
Qt::ItemIsEditable
with the flag
Qt::ItemIsEnabled
The first one tells Qt to create an editor for the value present in the model, which seems to be a texteditor in your case.
If the value is of type bool
then a dropdown list containing true
and false
would be shown instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With