I want to fill the viewport evenly of QListWidget which am using in iconMode to get grid layout for item.
Everything work fine but the problem is when resizing the mainWindow the listWidget waits to get the width so new item in the row can get space. Shown in the image below:

This is what i tried so far
void MainWindow::resizeEvent(QResizeEvent* event)
{
if(ui->listWidget->count()>0){
float items_that_can_fill_view = ui->listWidget->width() /(330+ui->listWidget->spacing());
float total_width_of_items = items_that_can_fill_view*330;
float remaining_width = ui->listWidget->width()-total_width_of_items;
float evenly_distributed_width = remaining_width/items_that_can_fill_view;
ui->listWidget->setGridSize(QSize(330+evenly_distributed_width,ui->listWidget->item(0)->sizeHint().height()));
}
event->accept();
}
In the above code am changing the gridSize at runtime of app by resizing the width of mainWindow. I choosed float instead of int as type to make changes very precisely. also 330 is the width of each item in listWidget.
Please help.
I'm using Qt 5.8.

