changing the default row size in a QTableView
Asked Answered
R

1

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.

Riva answered 18/12, 2014 at 16:59 Comment(5)
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. :-(Riva
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).Lanilaniard
Somehow I don't see a big difference between the second and third image.Somme
Look at the last blank row (below "toves")Riva
For anyone stumped by Jason's comment: the method in question is setSectionResizeMode, not setResizeMode.Alicyclic
M
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));
Messidor answered 18/12, 2014 at 17:6 Comment(2)
Hmmm... but I don't know the number of pixels. I just want the QTableView to display the data without the extra space.Riva
@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.Messidor

© 2022 - 2024 — McMap. All rights reserved.