Here is an example of the setData method in one of my models:
{
bool success = true;
if (index.isValid())
{
// get field name of index
QString fieldName
= mapped_dbFieldNames
[index.
column()];
JB_Node* node = nodeFromIndex(index);
if (role == Qt::CheckStateRole)
{
// Columns with a checkbox must be included here so it can be edited through the checkbox on the QtableView
if (editableCheckBoxDBFieldNamesList.contains(fieldName))
{
node->setData(fieldName, boolValue); // this is the important part
emit dataChanged(topLeft, bottomRight);
success = true;
}
}
else
{
node->setData(fieldName, value);
emit dataChanged(topLeft, bottomRight);
success = true;
}
}
else
success = false;
return success;
}
bool JB_NodeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool success = true;
if (index.isValid())
{
// get field name of index
QString fieldName = mapped_dbFieldNames[index.column()];
JB_Node* node = nodeFromIndex(index);
if (role == Qt::CheckStateRole)
{
// Columns with a checkbox must be included here so it can be edited through the checkbox on the QtableView
if (editableCheckBoxDBFieldNamesList.contains(fieldName))
{
QVariant boolValue = QVariant(value.toBool());
node->setData(fieldName, boolValue); // this is the important part
QModelIndex topLeft = index;
QModelIndex bottomRight = index;
emit dataChanged(topLeft, bottomRight);
success = true;
}
}
else
{
node->setData(fieldName, value);
QModelIndex topLeft = index;
QModelIndex bottomRight = index;
emit dataChanged(topLeft, bottomRight);
success = true;
}
}
else
success = false;
return success;
}
To copy to clipboard, switch view to plain text mode
Added after 5 minutes:
Also
you only need
flags |= Qt::ItemIsUserCheckable;
flags |= Qt::ItemIsUserCheckable;
To copy to clipboard, switch view to plain text mode
in your flags method to make your checkboxes checkable.
if you don't want your checkbox columns/fields to be editable, then use
flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsEnabled;
flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsEnabled;
To copy to clipboard, switch view to plain text mode
for those fields.
Bookmarks