CatamountJack 2024-05-15 03:09
I initially used a checkbox delegate in a QTableView I created back in PyQt 4.8 or PySide 1.2.1. It worked fine in PySide2 as well, but when I tried to update my application to PySide6, it stopped working (I can't remember exactly what the issue was). There's an updated version in PyQt5 available here: https://stackoverflow.com/a/50314085/224310, which basically works, except the delegate is a subclass of QItemDelegate
instead of QStyledItemDelegate
, and the checkboxes are rendered very large. Others seem to have encountered the same issue, as I found here: https://forum.qt.io/topic/142124/pyqt-checkbox-resolution-for-different-scales, but the poster mentioned they ultimately solved their problem by 'using Qt.ItemIsUserCheckable
instead of a delegate to implement checkboxes.' I tried the same approach, and it worked, but the checkboxes implemented with Qt.ItemIsUserCheckable
don't seem to be centered in the column of the QTableView. To center them, you just need... a delegate. I've spent a lot of time researching and trying different solutions, and what surprises me is how difficult it seems to be in Python to achieve the seemingly common use case of centering checkboxes in a column of a QTableView. (Well, I've been using Qt long enough that I'm not really surprised.) Does anyone have any suggestions on how to implement a column-centered checkbox in PySide6 that doesn't look so bad at high resolutions? (I'll answer my own question and post my final solution below, but I also welcome other (better) suggestions!)
Problem solving
Related Q&A