Here the actual code which is well reduced but still use the constant 20 :
virtual QSize sizeHint
( const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const override
{
// Unused.
DE_UNUSED( index );
// Constants.
const int RightMargin = 20;
const int ItemWidth = option.decorationSize.width() + ItemPadding.width();
// Get the list widget using the parent value.
// Compute the max width and the row item count.
const int MaxWidth = ListWidget->viewport()->width() - RightMargin;
const int RowItemCount = std::max( 1, MaxWidth / ItemWidth );
// Cases where the padding offset is not needed.
if( ( ItemWidth >= MaxWidth ) || ( RowItemCount >= ListWidget->count() ) )
return option.
decorationSize + QSize( 0, option.
fontMetrics.
height() ) + ItemPadding;
// Compute the padding offset and add in the item size to compensate of empty space.
const int PaddingOffset = ( MaxWidth - ( RowItemCount * ItemWidth ) ) / RowItemCount;
return option.
decorationSize + QSize( PaddingOffset, option.
fontMetrics.
height() ) + ItemPadding;
}
virtual QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const override
{
// Unused.
DE_UNUSED( index );
// Constants.
const int RightMargin = 20;
const QSize ItemPadding = QSize( 6, 6 );
const int ItemWidth = option.decorationSize.width() + ItemPadding.width();
// Get the list widget using the parent value.
QListWidget* ListWidget = static_cast< QListWidget* >( parent() );
// Compute the max width and the row item count.
const int MaxWidth = ListWidget->viewport()->width() - RightMargin;
const int RowItemCount = std::max( 1, MaxWidth / ItemWidth );
// Cases where the padding offset is not needed.
if( ( ItemWidth >= MaxWidth ) || ( RowItemCount >= ListWidget->count() ) )
return option.decorationSize + QSize( 0, option.fontMetrics.height() ) + ItemPadding;
// Compute the padding offset and add in the item size to compensate of empty space.
const int PaddingOffset = ( MaxWidth - ( RowItemCount * ItemWidth ) ) / RowItemCount;
return option.decorationSize + QSize( PaddingOffset, option.fontMetrics.height() ) + ItemPadding;
}
To copy to clipboard, switch view to plain text mode
If a way to have it really better and still working good exists, i'm open to listen how solve that.
Bookmarks