I was using a Checkbox Delegate in my QTableView originally created from around the time of PyQt 4.8 or PySide 1.2.1. It was also working in PySide2, but when I tried to update my application to PySide6, it no longer worked (I now no longer remember exactly what the problem was).
A newer version was posted for PyQt5 at /a/50314085/224310 which mostly worked except that the delegate was a subclass of QItemDelegate
instead of QStyledItemDelegate
the checkboxes were drawn horribly large. Others seemed to have the same issue, as I found at https://forum.qt.io/topic/142124/pyqt-checkbox-resolution-for-different-scales, but the poster indicated they ultimately solved their problem “by implementing the checkboxes through Qt.ItemIsUserCheckable
, rather than through a delegate”. I tried the same and it worked, but it seems that checkboxes implemented through Qt.ItemIsUserCheckable
are not able to be centered in a column of a QTableView. To center them you need… a delegate.
I’ve spent hours upon hours researching and trying different solutions and it amazes me that what seems like a common use-case of a column of centered checkboxes in a QTableView is so difficult to implement in Python. (Ok, I’ve worked with Qt long enough that I’m not really amazed.)
Does anyone have any suggestions on how to implement a column of check boxes in PySide6 that are centered in a column and do not look horrible at large resolutions?
(I’ll be answering my own question and posting my ultimate solution below, but I’d welcome other (better) suggestions as well!)