Alundra
18th June 2014, 19:39
Hi all,
I want to resize icon dynamically when the view resize.
Here the actual code who works :
virtual void resizeEvent( QResizeEvent* e )
{
QFileSystemModel* Model = static_cast< QFileSystemModel* >( model() );
QDir RootDir = QDir( Model->filePath( rootIndex() ) );
RootDir.setFilter( Model->filter() );
RootDir.setNameFilters( Model->nameFilters() );
RootDir.refresh();
const int ItemSize = 128 + 16;
const int ItemRow = std::ceil( (float)e->size().width() / (float)ItemSize );
if( ItemRow > (int)RootDir.count() )
{
setIconSize( QSize( 128, 128 ) );
}
else
{
if( ItemRow > 0 )
{
const int SizeOffset = ( ItemSize - ( e->size().width() % ItemSize ) ) / ItemRow;
const int IconSize = ( 128 - SizeOffset <= 0 ) ? 128 : 128 - SizeOffset;
setIconSize( QSize( IconSize, IconSize ) );
}
else
{
setIconSize( QSize( 32, 32 ) );
}
}
QListView::resizeEvent( e );
}
Can it be better ? One flickering can be seen who is not good.
This flickering is because I only set icon 128x128 or it's problem of the code ?
The max icon size is 128x128 and I add 16 to have it correct about margin :
const int ItemSize = 128 + 16;
Can the item size can be computed by code without hard coded values ?
Thanks for the help
I want to resize icon dynamically when the view resize.
Here the actual code who works :
virtual void resizeEvent( QResizeEvent* e )
{
QFileSystemModel* Model = static_cast< QFileSystemModel* >( model() );
QDir RootDir = QDir( Model->filePath( rootIndex() ) );
RootDir.setFilter( Model->filter() );
RootDir.setNameFilters( Model->nameFilters() );
RootDir.refresh();
const int ItemSize = 128 + 16;
const int ItemRow = std::ceil( (float)e->size().width() / (float)ItemSize );
if( ItemRow > (int)RootDir.count() )
{
setIconSize( QSize( 128, 128 ) );
}
else
{
if( ItemRow > 0 )
{
const int SizeOffset = ( ItemSize - ( e->size().width() % ItemSize ) ) / ItemRow;
const int IconSize = ( 128 - SizeOffset <= 0 ) ? 128 : 128 - SizeOffset;
setIconSize( QSize( IconSize, IconSize ) );
}
else
{
setIconSize( QSize( 32, 32 ) );
}
}
QListView::resizeEvent( e );
}
Can it be better ? One flickering can be seen who is not good.
This flickering is because I only set icon 128x128 or it's problem of the code ?
The max icon size is 128x128 and I add 16 to have it correct about margin :
const int ItemSize = 128 + 16;
Can the item size can be computed by code without hard coded values ?
Thanks for the help