Hi everybody,
I have a kind of working solution to have the items resized dynamically to fill the full space of QListWidget but in a very hacky way/ugly way and not that safe.
Here the code, you can see it resizes dynamically and adds the item when the row can add a new item in the row using a max item size:
Qt Code:
  1. class CDirectoryListWidgetCustomListDelegate
  2. : public QStyledItemDelegate
  3. {
  4. public:
  5. QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override
  6. {
  7. // Constants used for the size calcule.
  8. // 18px is the lowest value before flickering.
  9. // This value was found after experiments.
  10. const int RightMargin = 18;
  11. const QSize ItemPadding = QSize(6, 6);
  12. const int ItemWidth = option.decorationSize.width() + ItemPadding.width();
  13.  
  14. // Get the list widget using the parent value.
  15. const QListWidget* ListWidget = static_cast<QListWidget*>(parent());
  16.  
  17. // Compute the max width and the row item count.
  18. const int MaxWidth = ListWidget->viewport()->width() - RightMargin;
  19. const int RowItemCount = std::max(1, MaxWidth / ItemWidth);
  20.  
  21. // Cases where the padding offset is not needed.
  22. if ((ItemWidth >= MaxWidth) || (RowItemCount >= ListWidget->count()))
  23. return option.decorationSize + QSize(0, option.fontMetrics.height()) + ItemPadding;
  24.  
  25. // Compute the padding offset and add in the item size to compensate of empty space.
  26. const int PaddingOffset = (MaxWidth - (RowItemCount * ItemWidth)) / RowItemCount;
  27. return option.decorationSize + QSize(PaddingOffset, option.fontMetrics.height()) + ItemPadding;
  28. }
To copy to clipboard, switch view to plain text mode 
You can see that I have to play with a magic number to avoid flickering...
Any help is welcome if there is a possible way to achieve that correctly with Qt.
Thanks a lot!