QListView set height to show n rows
-
I have created a QListView, I want to set the fixed height to show 4 rows:
mplvRecs = new QListView(this); mpsiModel = QStandardItemModel(4,4,this); mplvRecs->setModel(mpsiModel); QFontMetrics fntMetrics(mplvRecs->fontMetrics()); int intHeight(fntMetrics.capHeight()); intHeight += mplvRecs->spacing(); mplvRecs->setFixedHeight(intHeight * 4);
Something isn't right, intHeight is 8, spacing is 0 and the list view shows barely large enough to display 2 rows, how can I fix this?
-
The cells do not only consist of the text (If that was the case the text of each row would touch at the top/bottom and it would look really messed up), but there is also some margin around it.
Therefore you cannot use fontMetrics to get the row height.
Why not use rowHeight() method to get row height? -
The cell is much higher than the text which is printed in there (as you can see when you look at your QListView). You should take a look at https://doc.qt.io/qt-5/qwidget.html#height-prop. Also there is some sacing around the actual view which you must consider.
-
The cells do not only consist of the text (If that was the case the text of each row would touch at the top/bottom and it would look really messed up), but there is also some margin around it.
Therefore you cannot use fontMetrics to get the row height.
Why not use rowHeight() method to get row height?