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
editorEvent
to handle mouse and keyboard events while setting the model with the appropriate state.and
Also see this similar question here...
This is the solution I came up with. This is assuming that you want the checkbox to be the only thing in the cell.
Solution for Python (PySide, PyQt) to center the checkbox and with editable allowed:
In your table model, make sure that the flags allow the user to check/uncheck the cell.
Finally, set the
BooleanDelagate
in your tableAlso you can look at this thread: http://www.qtcentre.org/threads/19157-QTableView-checkbox-center-with-stylesheet?p=181413#post181413
TextAlignmentRole really does mean what it says. Unfortunately, as you probably noticed, there doesn't seem to be any Icon/Widget alignment role available at all.
Bug report: http://bugreports.qt-project.org/browse/QTBUG-9047
Same question with some answers: http://lists.trolltech.com/qt-interest/2006-06/msg00476.html
Probably not the answer you're looking for, however I found it much easier to implement my own checkbox item delegate when using qtableviews.