문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top