0

I feel like I miss something extremely obvious, but couldn't find anything on it.

I have a custom item delegate for which I set the sizeHint height to 50, but the print statement returns a rectangle that is double the height.

def sizeHint(self, option, index):
    print("sizehint:", option.rect)
    s = QtCore.QSize()
    s.setWidth(option.rect.width())
    s.setHeight(50)
    return s

#output
sizehint: PySide2.QtCore.QRect(0, 0, 498, 100)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Vortek
  • 13
  • 5

1 Answers1

1

The "option.rect" is the rectangle that the view recommends taking into account the generic information that it has (for example the size of the font, the width of the header, etc.) that the delegate must take as reference for its painting or interaction, The rectangle does not take the information of each element (the information that you want to display) from time to time, so the delegate offers the sizeHint() as the recommended size. Actually if you want to get the default size then you should use super.

def sizeHint(self, option, index):
    default_size_hint = super().sizeHint(option, index)
    print("sizehint:", default_size_hint)
    return QtCore.QSize(option.rect.width(), 50)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Sorry, no matter how often I read your reply, I don't understand it. Also I don't get why the option.rect that is passed to the paint method is completely different than what is produced by sizeHint. – Vortek Apr 23 '20 at 14:07
  • @Vortek I explain with a trivial example: Let's say that the header of a column is "StackOverflow" so the minimum size (and by default) takes as a reference the length of that word (that length depends on the font) but the text of an item of that column is "SO" so obviously the length required for painting is different since the length of "StackOverflow" and "SO", the first is used to calculate "option.rect" and the second for sizeHint (the default value since any value can be set if that method is overridden). – eyllanesc Apr 23 '20 at 14:26
  • @Vortek If even with the example you don't understand it then you will have to check the source code: https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/itemviews/qstyleditemdelegate.cpp?h=dev#n402 – eyllanesc Apr 23 '20 at 14:27