I’m trying to create a table where each item contains a pair of rows, like this:

Ideally, the font of the top row would be different (rather than just bolded.)
My solution is to subclass QItemDelegate and overload paint and sizeHint. The above diagram show paint working properly, but I haven’t been able to figure out sizeHint. Here’s what I tried (just doing elements with a single row for now):
def sizeHint(self, option, index):
index_data = index.data(Qt.SizeHintRole)
if index_data is None:
return QSize()
size = QItemDelegate.textRectangle(None,
textLayoutBounds(option),
option.font, str(index_data))
return size
Unfortunately, with PyQt, the protected member function textRectangle (called by the default sizeHint) is inaccessible. What’s the correct way to do this?
I don’t have a complete solution to your question. However, for computing text rectangles you could take a look at QFontMetrics.boundingRect. Also, you might be interested in the rich-text item delegate given in this answer.