5

I've got a simple QTableView and I essentially want to emulate this:

view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)

However, I cannot actually use this, since this calls sizeHint on every single row, which is unacceptable in my case, since the widget needs to be responsive on startup and the calculations take a long time for all the rows. Therefore this is not an option.

I know what the size of a single column, which is always the same, and is always the one with largest height. However, the width of this cell may not always be the widest of all the cells in a row. I've made those cells the correct size by doing this, but this obviously makes all the cells the same size, and many are now far too wide.

view.horizontalHeader().setDefaultSectionSize(200)
view.verticalHeader().setDefaultSectionSize(100)

I've also tried to set individual column widths, but that seems to have no effect at all on the widths, like this:

view.setColumnWidth(0, 5)

Also, setting individual column widths isn't that great, since I can't know in advance how wide they're going to be.

Is there any way to use (or emulate) the ResizeToContents approach (as shown above), but in a way that wouldn't require checking all the cell sizes?

Pavlin
  • 5,390
  • 6
  • 38
  • 51

1 Answers1

3

Have you tried to optimize the ResizeToContents mode with setResizeContentsPrecision ?

Sets how precise QHeaderView should calculate the size when ResizeToContents is used. A low value will provide a less accurate but fast auto resize while a higher value will provide a more accurate resize that however can be slow.

The number precision specifies how many sections that should be consider when calculating the preferred size.

The default value is 1000 meaning that a horizontal column with auto-resize will look at maximum 1000 rows on calculating when doing an auto resize.

Special value 0 means that it will look at only the visible area. Special value -1 will imply looking at all elements.

Community
  • 1
  • 1
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • This is exactly what I was looking for, thank you very much. Just one follow-up question. This takes care of the column widths perfectly, however, if I try to do the same for the vertical header, it still calls `sizeHint` on all the rows. This isn't really a problem in this case, since I know what height I want, however I don't understand why this doesn't only look at the visible rows (is it that it doesn't know how many rows will be visible?). Furthermore, if I set the vertical header precision to ex. 2, all the rows are still checked. Any insights? – Pavlin Jul 08 '17 at 07:28
  • @Pavlin, glad you find some fix with this way. I used it as you did, without apply to the rows's height, so i can't tell you much more. You observed the same behavior with -1, 0 or a little number ? – PRMoureu Jul 08 '17 at 12:40
  • I tried various different values, but they all resulted in having to check all the rows. Thanks anyways! – Pavlin Jul 08 '17 at 13:02