Results 1 to 2 of 2

Thread: Dynamically resize item of QListWidget

  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

    Default Dynamically resize item of QListWidget

    Hi,
    Here the actual code I use, using a fixed value of 128 for the max icon size :
    Qt Code:
    1. class CDynamicIconListWidget : public QListWidget
    2. {
    3. public:
    4.  
    5. CDynamicIconListWidget( QWidget* Parent = nullptr ) :
    6. QListWidget( Parent )
    7. {
    8. }
    9.  
    10. protected:
    11.  
    12. virtual void resizeEvent( QResizeEvent* e )
    13. {
    14. QListWidget::resizeEvent( e );
    15. const int MaxItemSize = 128 + 16;
    16. const int ItemRow = ( e->size().width() / MaxItemSize ) + 1;
    17. if( ItemRow > count() )
    18. {
    19. setIconSize( QSize( 128, 128 ) );
    20. }
    21. else
    22. {
    23. if( ItemRow > 0 )
    24. {
    25. const int SizeOffset = ( MaxItemSize - ( e->size().width() % MaxItemSize ) ) / ItemRow;
    26. const int IconSize = ( 128 - SizeOffset <= 0 ) ? 128 : 128 - SizeOffset;
    27. setIconSize( QSize( IconSize, IconSize ) );
    28. }
    29. else
    30. {
    31. setIconSize( QSize( 32, 32 ) );
    32. }
    33. }
    34. }
    35. };
    To copy to clipboard, switch view to plain text mode 
    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 :
    Qt Code:
    1. const int MaxItemSize = 128 + 16;
    To copy to clipboard, switch view to plain text mode 
    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...
    Last edited by Alundra; 3rd January 2016 at 17:48.

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

    Default Re: Dynamically resize item of QListWidget

    Maybe a better solution is to make a custom QItemDelegate to paint the item based on the grid size and change the grid size when the QListWidget resize.

Similar Threads

  1. Replies: 1
    Last Post: 12th March 2015, 07:41
  2. Layout dynamically resize
    By gpetrous in forum Qt Programming
    Replies: 0
    Last Post: 18th July 2013, 12:04
  3. Dynamically resize text
    By antialias_forum in forum Qt Quick
    Replies: 4
    Last Post: 7th May 2011, 00:37
  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.