Alundra
3rd January 2016, 15:33
Hi,
Here the actual code I use, using a fixed value of 128 for the max icon size :
class CDynamicIconListWidget : public QListWidget
{
public:
CDynamicIconListWidget( QWidget* Parent = nullptr ) :
QListWidget( Parent )
{
}
protected:
virtual void resizeEvent( QResizeEvent* e )
{
QListWidget::resizeEvent( e );
const int MaxItemSize = 128 + 16;
const int ItemRow = ( e->size().width() / MaxItemSize ) + 1;
if( ItemRow > count() )
{
setIconSize( QSize( 128, 128 ) );
}
else
{
if( ItemRow > 0 )
{
const int SizeOffset = ( MaxItemSize - ( e->size().width() % MaxItemSize ) ) / ItemRow;
const int IconSize = ( 128 - SizeOffset <= 0 ) ? 128 : 128 - SizeOffset;
setIconSize( QSize( IconSize, IconSize ) );
}
else
{
setIconSize( QSize( 32, 32 ) );
}
}
}
};
That works good but more the widget is big more the offset on the right is big.
It's surely because of the hardcoded value for item size :
const int MaxItemSize = 128 + 16;
There is a way to compute the item size for a known icon size to avoid this hardcoded value ?
There is a better way or something needed to change in the actual code to have better result ?
Thanks for the help
EDIT:
Doesn't works if I change 128 by 64 for example...
Here the actual code I use, using a fixed value of 128 for the max icon size :
class CDynamicIconListWidget : public QListWidget
{
public:
CDynamicIconListWidget( QWidget* Parent = nullptr ) :
QListWidget( Parent )
{
}
protected:
virtual void resizeEvent( QResizeEvent* e )
{
QListWidget::resizeEvent( e );
const int MaxItemSize = 128 + 16;
const int ItemRow = ( e->size().width() / MaxItemSize ) + 1;
if( ItemRow > count() )
{
setIconSize( QSize( 128, 128 ) );
}
else
{
if( ItemRow > 0 )
{
const int SizeOffset = ( MaxItemSize - ( e->size().width() % MaxItemSize ) ) / ItemRow;
const int IconSize = ( 128 - SizeOffset <= 0 ) ? 128 : 128 - SizeOffset;
setIconSize( QSize( IconSize, IconSize ) );
}
else
{
setIconSize( QSize( 32, 32 ) );
}
}
}
};
That works good but more the widget is big more the offset on the right is big.
It's surely because of the hardcoded value for item size :
const int MaxItemSize = 128 + 16;
There is a way to compute the item size for a known icon size to avoid this hardcoded value ?
There is a better way or something needed to change in the actual code to have better result ?
Thanks for the help
EDIT:
Doesn't works if I change 128 by 64 for example...