1

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

enter image description here

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?

Neil G
  • 32,138
  • 39
  • 156
  • 257
  • Neither `textRectangle` nor `textLayoutBounds` are public methods of `QItemDelegate`, so you can't use them (obviously). But in any case, what is the purpose of attempting to compute `bottom_size`? – ekhumoro Dec 06 '11 at 01:16
  • `index_data` actually contains a list of strings. I want to calculate a rectangle that is as tall as the sum of the strings and as wide as the widest string. How should I do that? – Neil G Dec 06 '11 at 02:53
  • Also, `textRectangle` is protected, so I figured it would be available in a subclass? – Neil G Dec 06 '11 at 02:53
  • Why do you need to reimplement `sizeHint`? Can you provide some context? The code you've posted gives no clues at all. As for `textRectangle`: you can't access it because it is not part of Qt's public API, and hence not wrapped by PyQt. – ekhumoro Dec 06 '11 at 03:41
  • @ekhumoro: I've attached an image showing what I'm trying to do. – Neil G Dec 06 '11 at 03:55
  • I don't have a complete solution to your question. However, for computing text rectangles you could take a look at [QFontMetrics.boundingRect](http://doc.qt.nokia.com/latest/qfontmetrics.html#boundingRect-2). Also, you might be interested in the rich-text item delegate given in [this answer](http://stackoverflow.com/a/5443112/984421). – ekhumoro Dec 06 '11 at 14:32
  • @ekhumoro: Please add that as an answer. I have decided to make the table have twice as many rows and then fix the headers instead. – Neil G Dec 07 '11 at 00:25

1 Answers1

0

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.

Community
  • 1
  • 1
ekhumoro
  • 115,249
  • 20
  • 229
  • 336