Results 1 to 2 of 2

Thread: QListView resize icon dynamically

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation QListView resize icon dynamically

    Hi all,
    I want to resize icon dynamically when the view resize.
    Here the actual code who works :
    Qt Code:
    1. virtual void resizeEvent( QResizeEvent* e )
    2. {
    3. QFileSystemModel* Model = static_cast< QFileSystemModel* >( model() );
    4. QDir RootDir = QDir( Model->filePath( rootIndex() ) );
    5. RootDir.setFilter( Model->filter() );
    6. RootDir.setNameFilters( Model->nameFilters() );
    7. RootDir.refresh();
    8. const int ItemSize = 128 + 16;
    9. const int ItemRow = std::ceil( (float)e->size().width() / (float)ItemSize );
    10. if( ItemRow > (int)RootDir.count() )
    11. {
    12. setIconSize( QSize( 128, 128 ) );
    13. }
    14. else
    15. {
    16. if( ItemRow > 0 )
    17. {
    18. const int SizeOffset = ( ItemSize - ( e->size().width() % ItemSize ) ) / ItemRow;
    19. const int IconSize = ( 128 - SizeOffset <= 0 ) ? 128 : 128 - SizeOffset;
    20. setIconSize( QSize( IconSize, IconSize ) );
    21. }
    22. else
    23. {
    24. setIconSize( QSize( 32, 32 ) );
    25. }
    26. }
    27. QListView::resizeEvent( e );
    28. }
    To copy to clipboard, switch view to plain text mode 
    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 :
    Qt Code:
    1. const int ItemSize = 128 + 16;
    To copy to clipboard, switch view to plain text mode 
    Can the item size can be computed by code without hard coded values ?
    Thanks for the help
    Last edited by Alundra; 18th June 2014 at 20:55.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QListView resize icon dynamically

    Can it be better ? One flickering can be seen who is not good.
    The flickering is probably occurring because your implementation of resizeEvent() results in repainting the entire QListView for every mouse movement. If you can wait to redraw the view with new icon sizes until *after* the user has finished resizing, then one way to do it is like this:

    - Add a one-shot QTimer instance to your listview class. Connect a slot to the timeout() signal. In that slot, do all the things you are now doing in the resize event.

    - When the resizeEvent() is entered for the first time, start the timer with a short timeout (250 or 500 ms)
    - On each resize event, check to see if the timer is running. If it is, stop it and restart it. Don't do anything else in the event (except maybe call the base class resizeEvent).

    So, the way this will work is that nothing will happen to repaint the view with different icons until after the user has stopped moving the mouse (and the QTimer times out). When the QTimer fires, the view will be redrawn once with the new icons. If you are updating many icons, then you should call updatedEnabled( false) on your list view before starting the changes, then call updatesEnabled( true ) after you are all done.

Similar Threads

  1. Layout dynamically resize
    By gpetrous in forum Qt Programming
    Replies: 0
    Last Post: 18th July 2013, 12:04
  2. Dynamically resize text
    By antialias_forum in forum Qt Quick
    Replies: 4
    Last Post: 7th May 2011, 00:37
  3. How to resize the QIcon inside QPushButton dynamically?
    By gboelter in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2010, 13:34
  4. Dynamically resize spacing
    By trskel in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.