Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. pyqt checkbox resolution for different scales

pyqt checkbox resolution for different scales

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
7 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andverdu
    wrote on last edited by
    #1

    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)

    checkboxes at 125%
    checkboxes at 100%

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andverdu
      wrote on last edited by andverdu
      #7

      Issue solved by implementing the checkboxes through Qt.ItemIsUserCheckable, rather than through a delegate

      1 Reply Last reply
      0
      • F Online
        F Online
        friedemannkleint
        wrote on last edited by
        #2

        Which Qt version is this ? - This should improve in recent versions due to https://bugreports.qt.io/browse/QTBUG-86344 .

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andverdu
          wrote on last edited by
          #3

          I am using PySide6 version 6.2.1

          1 Reply Last reply
          0
          • F Online
            F Online
            friedemannkleint
            wrote on last edited by
            #4

            Please try the recent version (6.4.2/6.5.0).

            A 1 Reply Last reply
            0
            • F friedemannkleint

              Please try the recent version (6.4.2/6.5.0).

              A Offline
              A Offline
              andverdu
              wrote on last edited by
              #5

              @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?

              1 Reply Last reply
              0
              • F Online
                F Online
                friedemannkleint
                wrote on last edited by
                #6

                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 ).

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andverdu
                  wrote on last edited by andverdu
                  #7

                  Issue solved by implementing the checkboxes through Qt.ItemIsUserCheckable, rather than through a delegate

                  1 Reply Last reply
                  0
                  • A andverdu has marked this topic as solved on

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved