Disable Qt::CheckStateRole
-
I am developing a custom model derived from QAbstractTableModel. The model re-implements the
data()
,setData()
andflags()
methods as follows:QVariant TableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); // get the coordinates of the item int row = index.row(); int col = index.column(); // load the record pointed to by the row index for the current query Record recordLoaded = mDao->readRecordN(row, mCurrentQuery); // handle the different roles switch (role) { case Qt::DisplayRole: return readDisplayRoleData(col, recordLoaded); case Qt::EditRole: return readEditRoleData(col, recordLoaded); case Qt::BackgroundRole: return readBackgroundRole(col, recordLoaded); case Qt::CheckStateRole: return readCheckStateRoleData(col, recordLoaded); default: break; } return QVariant(); } bool TableModel::setData(const QModelIndex & index, const QVariant & value, int role) { if (!index.isValid()) return false; // get the coordinates of the item to be edited int row = index.row(); int col = index.column(); // load record to be edited Record recordEdited = mDao->readRecordN(row, mCurrentQuery); switch (role) { case Qt::EditRole: if (writeEditRoleData(col, recordEdited, value)) { mDao->updateRecord(recordEdited); // notify the view for the changes emit dataChanged(index, index); return true; } case Qt::BackgroundRole: if (writeBackgroundRole(col, recordEdited, value)) { // notify the view for the changes emit dataChanged(index, index); return true; } case Qt::CheckStateRole: if (writeCheckStateRoleData(col, recordEdited, value)) { // notify the view for the changes emit dataChanged(index, index); return true; } default: break; } return false; } Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { if (!index.isValid()) return Qt::ItemIsEnabled; int columnId = index.column(); switch (columnId) { case TableModel::Column1: case TableModel::Column2: case TableModel::Column3: case TableModel::Column4: default: return QAbstractItemModel::flags(index); break; } }
It is also important to note how the two methods
readCheckStateRoleData()
andwriteCheckStateRoleData()
are implemented:QVariant TableModel::readCheckStateRoleData(int columnId, Record& record) const { switch (columnId) { case TableModel::Column1: { bool isVisible2D = false; // Some code to read value of isVisible2D return (isVisible2D ? Qt::Checked : Qt::Unchecked); } case TableModel::Column2: { bool isVisible3D = false; // some code to read value of isVisible3D return (isVisible3D ? Qt::Checked : Qt::Unchecked); } case TableModel::Column3: case TableModel::Column4: default: return QVariant(); } } bool TableModel::writeCheckStateRoleData(int columnId, PlanImageRecord& record, const QVariant& value) { switch (columnId) { case TableModel::Column1: { bool isChecked = false; if ((Qt::CheckState)value.toInt() == Qt::Checked) isChecked = true; // code to save value in database return true; } case TableModel::Column2: { bool isChecked = false; if ((Qt::CheckState)value.toInt() == Qt::Checked) isChecked = true; // code to write new value in database return true; } case TableModel::Column3: case TableModel::Column4: default: return false; } }
The problem that I have is that although the
flags()
method for Column1 and Column2 does not set the flagQt::ItemIsUserCheckable
, the items of these columns appear as checkboxes. How can I avoid this while still handling theQt::CheckStateRole
role in thedata()
andsetData()
methods? Is this possible?The reason why I am trying to keep handling the
Qt::CheckStateRole
is because I am displaying the same model in two separateQTableView's
but in the second view the model is wrapped in a proxy model which modifies the flags such that the columns are editable or displayed as check-boxes where appropriate. In the first view, I only want the DisplayRole of the items to be shown. -
@ochampao said in Disable Qt::CheckStateRole:
Is this possible?
No since you return valid values for the Qt::CheckStateRole for those columns. When you don't want checkboxes then don't return a valid value for them.
The flag only defines if the use is able to modify the check state or not. -
You can use a QIdentityProxyModel