Results 1 to 5 of 5

Thread: QListWidget: Resize to it's content

  1. #1
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QListWidget: Resize to it's content

    Hi,

    I'm trying to resize a QListWidget to it's content with QFontMetrics, calculating the height and the width. The calculating of the width works perfectly, but it fails to calculate the right height.

    Qt Code:
    1. QListWidget *widget = new QListWidget(this);
    2. QFont font("Droid Sans", 12, false);
    3. QFontMetrics fm(font);
    4.  
    5. int width = fm.width() * longestEntry + 10;
    6. int height = fm.height() * widget.count() + 10;
    7.  
    8. widget->resize(width, height);
    To copy to clipboard, switch view to plain text mode 
    The problem is, that the space between the QListWidgetItems depends on the used font and QFontMetrics delivers only the height without the space.


    Is it possible to get these extra pixels so I can calculate the right height with
    Qt Code:
    1. int height = (fm.height() + pixels) * widget.count();
    To copy to clipboard, switch view to plain text mode 

    Or is there another (better) way to resize the QListWidget to it's content?

    Many thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget: Resize to it's content


  3. #3
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QListWidget: Resize to it's content

    Thank you for your answer! But it does not working as expected, with qDebug() I get the following
    Qt Code:
    1. qDebug() << fm.lineSpacing() << fm.leading() << fm.height()
    To copy to clipboard, switch view to plain text mode 

    16 -1 17

    I don't know why but lineSpacing() (16 pixel) gives a smaller value as the height (17 pixel). But to calculate the height of the QListWidget correctly, the value must be greater than 17 pixel for each entry.
    Any suggestions?

    Many thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget: Resize to it's content

    Hmm, strange.
    Have you tried if that is true for other fonts as well?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QListWidget: Resize to it's content

    I have tested it with different fonts and only a few gave positive values, all the rest gives -1.
    I'm using Qt 4.8.5 on Linux.

Similar Threads

  1. resize to content of a QTreeWidget
    By torres in forum Newbie
    Replies: 4
    Last Post: 23rd April 2013, 04:14
  2. Replies: 3
    Last Post: 11th December 2011, 10:09
  3. QListWidget resize performance issues
    By Talei in forum Newbie
    Replies: 0
    Last Post: 17th July 2010, 05:59
  4. Read SMS in-box content & show it in QListWidget?
    By damodharan in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 05:51
  5. Replies: 0
    Last Post: 28th December 2009, 12:24

Tags for this Thread

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.