QListWidget set height to fit n items?
-
I'm currently setting the width using:
QString strColumns = mpobjNode->strGetAttribute(clsXMLnode::mscszAttrColumns); if ( strColumns.isEmpty() != true ) { int intCharWidth = QFontMetrics(font()).maxWidth(); int intColumns = strColumns.toInt() * intCharWidth; setFixedWidth(intColumns); }
I want to set the height of the list widget to a specific number of rows, I tried:
QString strRows = mpobjNode->strGetAttribute(clsXMLnode::mscszAttrRows); if ( strRows.isEmpty() != true ) { int intCharHeight = strRows.toInt() * QFontMetrics(font()).capHeight(); setFixedHeight(intCharHeight); }
However clearly when using just 1 row the height is to small and doesn't even contain a row properly.
What is the correct way to do this?
I've also tried this:
QString strRows = mpobjNode->strGetAttribute(clsXMLnode::mscszAttrRows); if ( strRows.isEmpty() != true ) { QRect rctBounds = QFontMetrics(font()).boundingRect("X"); int intCharHeight = strRows.toInt() * rctBounds.height(); setFixedHeight(intCharHeight); }
What I'm missing is the spacing above and below the text, how do I get this?
I've done it, well almost:
QMargins margins = contentsMargins(); if ( strRows.isEmpty() != true ) { QRect rctBounds = QFontMetrics(font()).boundingRect("X"); int intHeight = (strRows.toInt() * rctBounds.height()) + margins.top() + margins.bottom(); setFixedHeight(intHeight); }
It still looks a few pixels mixing, comparing it with some LineEdit widgets which are obviously taller.
I've also tried:
QSize szHint = minimumSizeHint(); intHeight = minimumHeight();
szHint contains {74,74}
intHeight contains 0Why did someone downvote this post? Can't you post a reason why, if whoever did that knows the answer then post it and while your at it say why you're downvoting!
The correct height seems to be:
QRect rctBounds = QFontMetrics(font()).boundingRect("X"); int intHeight = (strRows.toInt() * rctBounds.height()) + margins.top() + margins.bottom() + 3; setFixedHeight(intHeight);
In the above:
strRows contains "1"
rctBounds.height() returns 15
margins.top() is 1
margins.bottom() is 1So without the 3 fiddle factor the height is 17, adding 3 is exactly the correct height, is there a proper way to get this?