pyqt checkbox resolution for different scales
-
wrote on 11 Jan 2023, 11:40 last edited by
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 methoddrawCheck
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
-
wrote on 13 Mar 2023, 09:51 last edited by andverdu
Issue solved by implementing the checkboxes through Qt.ItemIsUserCheckable, rather than through a delegate
-
wrote on 11 Jan 2023, 14:16 last edited by
Which Qt version is this ? - This should improve in recent versions due to https://bugreports.qt.io/browse/QTBUG-86344 .
-
wrote on 1 Feb 2023, 12:54 last edited by
I am using PySide6 version 6.2.1
-
wrote on 1 Feb 2023, 15:28 last edited by
Please try the recent version (6.4.2/6.5.0).
-
Please try the recent version (6.4.2/6.5.0).
wrote on 8 Feb 2023, 12:58 last edited by@friedemannkleint Thanks for your suggestion, I have tested with pyside 6.4.2 and the issue persists, do you have any other idea on how to solve this?
-
wrote on 9 Feb 2023, 11:47 last edited by
You can avoid fractional scale factor rounding by setting
QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.Round)
If it differs from a normal QCheckBox, I would file a bug under QTBUG (see also https://bugreports.qt.io/browse/QTBUG-86344 ).
-
wrote on 13 Mar 2023, 09:51 last edited by andverdu
Issue solved by implementing the checkboxes through Qt.ItemIsUserCheckable, rather than through a delegate
-