2
votes

I have a subclass of QListWidget, the widget holds text items in a single column and multiple rows (the usual kind). I want the widget to resize itself to the minimum size where the text items will still be visible. I've tried using the resize() method with the contentsSize() argument, this will resize the widget's height to fit the text contents, however the width stays the same.

Here's a snippet of an overriden method that I'm testing this with:

override void mousePressEvent(QMouseEvent event)
{
    this.resize(this.contentsSize());
}

Note: This is in the D language, and I'm using the QtD wrapper library. Unless I'm doing something wrong it might even be a QtD bug (but I doubt it).

1
The answer described in stackoverflow.com/questions/6337589/… might help.Vlado Klimovský
Thanks, Vlado. Now typing more than fifteen characters because of a silly stackoverflow limit. What a nice waste of my time. Anyway, thanks again!Andrej Mitrović

1 Answers

1
votes

If you're content to switch to a QTableView or QTreeView, you can call resizeColumnsToContents(), and resize your widget based on the resulting width. Otherwise you'll have to iterate over your QListWidget contents and get the maximum of the widths of the items.