6

How do you change the default row size in a QTableView so it is smaller? I can call resizeRowsToContents(), but then subsequent inserts still add new rows at the end which are the default size.

I am guessing it might have something to do with style sheets but I'm not familiar enough with what things impact the visual changes.

I am using PySide in Python, but I think this is a general Qt question.


example view:

default table appearance

enter image description here

what the table looks like after resizeRowsToContents():

enter image description here

now if I add a new blank row at the end:

enter image description here

Darn, it uses the default row height, with all that extra space.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • `tableView.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)` seems to works nicely, but it's dynamic and slows down my application when I'm doing lots of realtime updates. I really just want to get the standard row size. :-( – Jason S Dec 18 '14 at 20:12
  • 2
    The "standard" row height is what you get when you don't resize to contents. It's based on the size hint for the row, which is derived from the size hint for the item delegate (amongst other things). If you don't like that specific height, just change it to something more acceptable (e.g. `table.fontMetrics().height() + 6`). – ekhumoro Dec 18 '14 at 20:56
  • Somehow I don't see a big difference between the second and third image. – NoDataDumpNoContribution Jan 07 '15 at 09:28
  • Look at the last blank row (below "toves") – Jason S Jan 07 '15 at 14:37
  • For anyone stumped by Jason's comment: the method in question is `setSectionResizeMode`, not `setResizeMode`. – Kuba hasn't forgotten Monica Jun 24 '20 at 18:35

1 Answers1

14

Qt::SizeHintRole responsible for width and height of cell. So you can just:

someModel.setData(someIndex,QSize(width, height), Qt::SizeHintRole);

Or just this:

someView.verticalHeader().setDefaultSectionSize(10);

And I think that setDefaultSectionSize is exactly what are you looking for. AFAIK there is no another simple way.

From doc:

defaultSectionSize : int

This property holds the default size of the header sections before resizing. This property only affects sections that have Interactive or Fixed as their resize mode.

If you don't know how much pixels you need, then try to get this height after applying resizeRowsToContents() with rowHeight()

So it should be something like:

resizeRowsToContents();
someView.verticalHeader().setDefaultSectionSize(someView.rowHeight(0));
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Hmmm... but I don't know the number of pixels. I just want the QTableView to display the data without the extra space. – Jason S Dec 18 '14 at 17:27
  • @JasonS After your screenshot I can see, that row height is same, so I edited my answer and if data is not very complex, it should work. – Jablonski Dec 18 '14 at 18:54