Set height of QTableView to fit exact number of rows.
I have a QTableView where all rows have exactly the same height and I'd like to make the QTableView the exact height to fit a specific number of rows. Right now I'm using code like this to calculate the height needed:
QTableView *pTableView; // The tableview whose model is filled with data.
const int nNumRows = 10;
int nRowHeight = pTableView->rowHeight(0);
int nTableHeight =
(nNumRows * nRowHeight) +
pTableView->horizontalHeader()->height() +
2 * pTableView->frameWidth();
pTableView->setMinimumHeight(nTableHeight);
pTableView->setMaximumHeight(nTableHeight);
However, the height of the table still seems to be about 2 pixels shorter than what the rows need. Any idea what I might be missing?
Thanks!
Re: Set height of QTableView to fit exact number of rows.
I know what you mean. Quite irritating isn't it :(
I had the same problem a while ago and debated whether or not to fix it, but it turned out that the content of my table would always be more than what the height could show - so for me the scrollbar does the trick.
For interest's sake I would still like to see a nice solution to it.
Re: Set height of QTableView to fit exact number of rows.
Almost 8 years later and QT 4.8.4 still has this problem. Does not happen on Windows but does happen on Mac OS. If I do not add 2 more pixels I get a scroll bar once there is 4 rows visible.
This seems to be a difference between Windows and Mac. Adding 2 pixels to windows creates extra buffer that is not needed.
Re: Set height of QTableView to fit exact number of rows.
I tested on Windows 10 using Qt 5.10.1. I did not have this problem.