4
votes

I've got a QListWidget and I've got a custom widget class, which I'm creating an instance of for each item appearing in the list widget.

But I can't figure out how to make my custom widget expand to fill all the available space of each row in the list widget.

My custom widget is created in Qt Designer, and I've set its size hint to be expanding horizontally, but it doesn't do this when placed in my list widget (i.e. in designer, the widget is set to be say, 200px wide. The list widget is actually 400px wide, but my custom widget in each row does not expand to fill this space...)

How do I make this work?

Thanks.

2

2 Answers

3
votes

I'm assuming you're putting items in here using QListWidget::setItemWidget()?

If that's the case, then you usually need to set the sizeHint() of your QListWidgetItem to that of your custom widget:

myListWidget->setItemWidget( myListWidgetItem, customWidget );
myListWidgetItem->setSizeHint( customWidget->sizeHint() );
1
votes

As an update, since Qt 5.2. you can set the QListWidget's sizeAdjustPolicy:

listWidget-setSizeAdjustPolicy(QListWidget::AdjustToContents);

This would resize the QListWidget every time a new item is added to it (whose width exceeds the current width).

Reference: http://doc.qt.io/qt-5/qabstractscrollarea.html#sizeAdjustPolicy-prop