开发者

Making QListWidget resize its items to fill space

开发者 https://www.devze.com 2023-04-13 04:03 出处:网络
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.

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 t开发者_JS百科o 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号