Question

I have a QListWidget with some widgets inside:

QListWidgetItem* w = new QListWidgetItem(ui->listWidget);
ui->listWidget->addItem(w);
w->setSizeHint(widget->sizeHint());
ui->listWidget->setItemWidget(w, widget);

However, everything seems to work fine until I resize the the QListWidget (it is embedded into a QDockwidget). Do I have to iterate over all items and resize them manually or is there a simple trick?

It is just a list with items having a button on the left and a button on the right. If the list gets resized (QSize changes) than the buttons gets hidden and a scrollbar appears. I want to have the size of the item according to the list width.

Was it helpful?

Solution

Try setting the resizeMode property to Adjust. This will cause your QListWidget to automatically resize all of the items in the view to the size of the QListWidget anytime the widget is resized.

See the documentation here. Your code will be structured like so:

QListWidgetItem* w = new QListWidgetItem(ui->listWidget);
w->setSizeHint(widget->sizeHint());
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->addItem(w);
ui->listWidget->setItemWidget(w, widget);

EDIT: Depending on the uniformity of your items, you may also benefit from setting the uniformItemSizes property. QListView can optimize the layout of your view if you set this property.

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