Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget set height to fit n items?
Qt 6.12 LTS is out! See what's new in the release blog

QListWidget set height to fit n items?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 435 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    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 0

    Why 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 1

    So without the 3 fiddle factor the height is 17, adding 3 is exactly the correct height, is there a proper way to get this?

    Kind Regards,
    Sy

    1 Reply Last reply
    -1

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved