tableview - Pyside6 Issue with scaling factor of windows on high dpi screen - Stack Overflow

admin2025-04-18  3

I have this bit of code to draw a line on the upper border of specific row in a treeview:

class TextValidatorDelegate(QStyledItemDelegate):
   
    """a delegate to validate user input"""

    def __init__(self, parent = None):
        super().__init__(parent) 
        self.validator = QRegularExpressionValidator(QRegularExpression(R'[a-zA-Z0-9_]*'), self)

    #issue with hdpi screen
    def paint(self, painter, option, index):
        super().paint(painter, option, index)
        if index.data(customDataRole.WellRow):
            rect = option.rect
            painter.setPen(QPen())
            indent = rect.left() - 50 # -50 to draw line over the expand/collapse decorations of parent items
            painter.drawLine(indent, rect.top(), rect.right(), rect.top())
...

On high dpi screen when window scaling factor is set to anything other than 100%, the lines are 2px wide at start instead of 1 px. Moreover they, keep switching between 0px 1px 2px width whenever the item is hovered, or when scrolling. I have tried various options

  • with QApplication.setAttribute(Qt.AA_DisableHighDpiScaling)
  • with stylesheet : item{border : 1px solid;}
  • with os import os.environ["QT_SCALE_FACTOR"] = "1"
  • with config files

And nothing changes on the high dpi monitor. The only solution I found was to disable it in windows settings. Is there an other way to draw these lines so that Windows does not try to scale them? or is there a way to stop window scaling the app (from within the app code) Thank in advance

I have this bit of code to draw a line on the upper border of specific row in a treeview:

class TextValidatorDelegate(QStyledItemDelegate):
   
    """a delegate to validate user input"""

    def __init__(self, parent = None):
        super().__init__(parent) 
        self.validator = QRegularExpressionValidator(QRegularExpression(R'[a-zA-Z0-9_]*'), self)

    #issue with hdpi screen
    def paint(self, painter, option, index):
        super().paint(painter, option, index)
        if index.data(customDataRole.WellRow):
            rect = option.rect
            painter.setPen(QPen())
            indent = rect.left() - 50 # -50 to draw line over the expand/collapse decorations of parent items
            painter.drawLine(indent, rect.top(), rect.right(), rect.top())
...

On high dpi screen when window scaling factor is set to anything other than 100%, the lines are 2px wide at start instead of 1 px. Moreover they, keep switching between 0px 1px 2px width whenever the item is hovered, or when scrolling. I have tried various options

  • with QApplication.setAttribute(Qt.AA_DisableHighDpiScaling)
  • with stylesheet : item{border : 1px solid;}
  • with os import os.environ["QT_SCALE_FACTOR"] = "1"
  • with config files

And nothing changes on the high dpi monitor. The only solution I found was to disable it in windows settings. Is there an other way to draw these lines so that Windows does not try to scale them? or is there a way to stop window scaling the app (from within the app code) Thank in advance

Share Improve this question edited Jan 31 at 11:09 Eric Bayard asked Jan 29 at 15:35 Eric BayardEric Bayard 114 bronze badges 1
  • This question is similar to: PyQt 5 and 4k screen. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – musicamante Commented Feb 1 at 2:34
Add a comment  | 

1 Answer 1

Reset to default -1

from qt io forum I found that [QT SCALE FACTOR] is useless, as it acts a a multiplier of ms-window scale factor, so setting it to 1 has no effects. And setting it to any other value will work even on none high dpi screen, which will shrink/expand everything. The working option is : os.environ["QT_ENABLE_HIGHDPI_SCALING"]= "0"

转载请注明原文地址:http://www.anycun.com/QandA/1744955927a89996.html