I am using QTableView’s checkbox flag of Qt::ItemIsUserCheckable to display a checkbox in a table cell.
After reading some things on alignment in an attempt to center the checkbox within the cell, I am returning the Qt::AlignCenter as the TextAlignmentRole from the models data() function.
QVariant ExampleModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole)
return Qt::AlignCenter | Qt::AlignVCenter;
}
This however is not aligning my checkbox.
Does anyone know how to align checkboxes is this mode?
After further investigation into delegate options I found a nice reference (unfortunately no longer available) and came up with the following hybrid using a QItemDelegate and IsUserCheckable.
Essentially, you need to extend QItemDelegate, and reimplement, using the drawCheck function to center and use the
editorEventto handle mouse and keyboard events while setting the model with the appropriate state.and
Also see this similar question here…