I am developing a windows app with pyside6. I have a QTableView and have used a QItemDelegate to add checkboxes to the table. The checkboxes are drawn in the paint
method of the QItemDelegate, using the method drawCheck
class CheckBoxDelegate(QItemDelegate):
"""
A delegate that places a fully functioning QCheckBox cell of the column to which it's applied.
"""
def __init__(self, parent):
QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
"""
Important, otherwise an editor is created if the user clicks in this cell.
"""
return None
def paint(self, painter, option, index):
"""
Paint a checkbox without the label.
"""
value = int(index.data())
if value == 0:
value = Qt.Unchecked
else:
value = Qt.Checked
self.drawCheck(painter, option, option.rect, value)
def editorEvent(self, event, model, option, index):
"""
Change the data in the model and the state of the checkbox
if the user presses the left mousebutton and this cell is editable. Otherwise do nothing.
"""
if not int(index.flags() & Qt.ItemIsEditable) > 0:
return False
if event.type() == QEvent.MouseButtonRelease and event.button() == Qt.LeftButton:
# Change the checkbox-state
self.setModelData(None, model, index)
return True
return False
def setModelData(self, editor, model, index):
"""
Set new data in the model
"""
model.setData(index, 1 if int(index.data()) == 0 else 0, Qt.EditRole)
The problem is that the checkboxes are only correctly displayed if the scale is set at 100%. Changing it to 125% or more, makes the checkbox too big and the icon inside blurry (images below)
I have tried different settings for scaling the app. The combination of settings that works to scale the rest of the application is:
os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
os.environ["QT_FONT_DPI"] = "96"
os.environ["QT_SCALE_FACTOR"] = "1"
QApplication.setHighDpiScaleFactorRoundingPolicy(QtCore.Qt.HighDpiScaleFactorRoundingPolicy.Floor)
Still changing these settings has no effect on the checkboxes resolution