Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing the default row size in a QTableView

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.

like image 751
Jason S Avatar asked Dec 15 '22 17:12

Jason S


1 Answers

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));
like image 61
Kosovan Avatar answered Dec 28 '22 12:12

Kosovan