Question

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.

Was it helpful?

Solution

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() );

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top