Results 1 to 6 of 6

Thread: QListWidget resize item to reproduce Windows behavior : Magical Number needed

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 QListWidget resize item to reproduce Windows behavior : Magical Number needed

    Hi,
    Here my solution to reproduce the Windows behavior to have always the QListWidget filled in space (override of QStyledItemDelegate) :
    Qt Code:
    1. virtual QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const override
    2. {
    3. const int RightMargin = 15;
    4. const QSize ItemPadding = QSize( 6, 6 );
    5. QListWidget* ListWidget = static_cast< QListWidget* >( parent() );
    6. const int MaxWidth = ListWidget->viewport()->width() - RightMargin;
    7. const int ItemWidth = option.decorationSize.width() + ItemPadding.width();
    8. if( ItemWidth >= MaxWidth )
    9. return option.decorationSize + QSize( 0, option.fontMetrics.height() ) + ItemPadding;
    10. const int ItemHeight = option.decorationSize.height() + option.fontMetrics.height() + ItemPadding.height();
    11. const int NumItem = std::max( 1, MaxWidth / ItemWidth );
    12. const int NumRow = std::ceil( static_cast< float >( ListWidget->count() ) / static_cast< float >( NumItem ) );
    13. const bool ScrollbarVisible = ( NumRow * ItemHeight ) > ListWidget->viewport()->height();
    14. if( ScrollbarVisible )
    15. {
    16. const int PaddingOffset = ( NumItem >= ListWidget->count() ) ? 0 : ( MaxWidth - ( NumItem * ItemWidth ) ) / NumItem;
    17. return option.decorationSize + QSize( 0, option.fontMetrics.height() ) + ItemPadding + QSize( PaddingOffset, 0 );
    18. }
    19. else
    20. {
    21. // 3 pixels removed to avoid flickering when resize.
    22. const int NewMaxWidth = ListWidget->viewport()->width() - RightMargin - 3;
    23. const int NewNumItem = std::max( 1, NewMaxWidth / ItemWidth );
    24. const int PaddingOffset = ( NewNumItem >= ListWidget->count() ) ? 0 : ( NewMaxWidth - ( NewNumItem * ItemWidth ) ) / NewNumItem;
    25. return option.decorationSize + QSize( 0, option.fontMetrics.height() ) + ItemPadding + QSize( PaddingOffset, 0 );
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    This code compute the free space on the right based on the known item size and distribute in each item to always be filled on the width.
    All works fine but why this 3px magical number is needed to avoid flickering when the scrollbar is not visible ?
    If this magical number is not used one item always swap from one line to another when resize.
    Thanks
    Last edited by Alundra; 16th January 2016 at 03:24.

Similar Threads

  1. Dynamically resize item of QListWidget
    By Alundra in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2016, 22:20
  2. Replies: 1
    Last Post: 11th November 2015, 13:14
  3. QSplitter resize behavior
    By Alundra in forum Qt Programming
    Replies: 0
    Last Post: 5th June 2014, 21:25
  4. Strange resize behavior
    By Lykurg in forum Newbie
    Replies: 3
    Last Post: 9th January 2007, 13:56
  5. QListWidget selection behavior as in Windows XP
    By Levon Nikoghosyan in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 13:11

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
  •  
Qt is a trademark of The Qt Company.