Fit QListWidget to content

  • What's the recommended technique to fit a QListWidget to the width/height of its content? I'd like to have a dynamic list widget that uses no scrollbars and always shows all its elements. I need to know what to resize to prior to showing the ListWidget.

    Why is "contentsRect" for a ListWidget returning clearly inaccurate information? even when called for an initialized and visible list widget, full of my own rows? It's ~ 30% wrong on both height and width.

    Is there any API call that's accurate?

  • I ended up using a combination of item.count * GridSize.height for the height and a QFontMetrics to evaluate the width of the text, with some hard coded provisions for the icon size i'll be using + some extra padding.

    Still, I can't help feeling this is a hack. Am I missing some 'right way' of doing this?

  • I currently have no idea on how to get the size of the content. What could work is to get the size of the data model, but then you are missing the sizes that are added by the view (e.g. spaces between items etc.).

  • Hi,

    is there no solution for this problem yet?

    I have a QListView which is filling using a model. But my QlistView height is far greater than the size of the contents. In other words, even it consist only two items it shows listview with amount of space for 10 or 15 items. I want to fit the size of the ListView to contents.

    How can I do that?


Log in to reply