QTableView not displaying check box for column zero
-
In my TableModel I have defined a flags() mf :
Qt::ItemFlags flags(const QModelIndex& index) const override { auto flags = QAbstractTableModel::flags(index); if (0 == index.column()) flags |= Qt::ItemIsUserCheckable; return flags; }
in my data() mf I have:
if (role == Qt::CheckStateRole) { if (0 == index.column()) return file.m_bChecked; else return QVariant(); }
where m_bChecked is of type QT::CheckState
But I never see a check box added to column 0?
Did I miss some crucial step?
Thanks
David -
@JonB Thanks for that pointer ... I'll look right away
The code now reads:
else if (Qt::CheckStateRole == role && 0 == index.column()) { file.m_bChecked = static_cast<Qt::CheckState>(value.toInt()); emit dataChanged(index, index); return true; }
which seems to fix the problem ...
Thanks a lot
-
Put a debug statement in your data() / Qt::CheckStateRole and see if you get there. Also make sure m_bChecked is really of type Qt::CheckState.
The flags() is only needed when you want the data to be modified by the user ('UserCheckable') -
OK that found the problem quickly enough - there was a spurious return QVariant() before the check for CheckStateRole. <BLUSH>
However, now that's fixed, I cannot change the value of the check box by interacting with it - do I need to write some extra code to allow user interaction with that check box?
Thanks
David -
@JonB Thanks for that pointer ... I'll look right away
The code now reads:
else if (Qt::CheckStateRole == role && 0 == index.column()) { file.m_bChecked = static_cast<Qt::CheckState>(value.toInt()); emit dataChanged(index, index); return true; }
which seems to fix the problem ...
Thanks a lot