Results 1 to 4 of 4

Thread: QLabel: Resize font to contentsRect

  1. #1
    Join Date
    Jul 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QLabel: Resize font to contentsRect

    Hi,

    I try to implement an algorithm to resize the font of a QLabel, so that the text fits. For that I am using contentsRect() and QFontMetrics. I just decrease the font size as long as the boundingRect is bigger than the contentsRect. This works fine as long as the frameShape is NoFrame. As soon as I change it to StyledPanel (and I have to, because I want to use Stylesheets), it no longer works. It seems as if the area for the content is always a bit smaller than contentsRect.

    To check this I tried the following scenario:
    Under Windows a label with fontsize 12 and the text "this is a test" with active word wrap and MS sans serif. QFontMetrics::width("this is a test") returns 84. So I conclude that the contentsRects needs a width of at least 84px to hold the whole text in one line and avoid any word wrap.
    But in fact it needs to be 87px to do so. In this case the label width is 89px, the left and righ contentsMargins are each 1px. So contentsRects seems to be as expected. Furthermore the difference between expected and necessary width seems to depend on the fontsize. For example for Fontsize 24 and the same scenario, I need 7 additional pixels.

    The word wrap is just to visualize, if the text fits. If I turn word wrap off and set the contentsRects width to 84px, the text is cut off.

    Does anyone know, why it behaves like that? Or does anyone have another solution to implement this feature?

    Thanks and best regards,
    Ghal_Maraz

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

    Default Re: QLabel: Resize font to contentsRect

    If you look at the implementation of QLabel:aintEvent() you'll see that it uses the contents rect with additional label margin removed:
    https://code.woboq.org/qt5/qtbase/sr...P11QPaintEvent

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Ghal_Maraz (27th July 2016)

  4. #3
    Join Date
    Jul 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLabel: Resize font to contentsRect

    Thanks for your reply.

    I checked the margin and it is configured to zero and also label's margin() returns zero.
    I think it must be somehow connected to StyledPanel as frameShape.

  5. #4
    Join Date
    Jul 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLabel: Resize font to contentsRect

    I finally found the reason for this behavior in the function QLabelPrivate::documentRect in qlabel.cpp:
    Qt Code:
    1. int m = indent;
    2. if (m < 0 && q->frameWidth()) // no indent, but we do have a frame
    3. m = q->fontMetrics().width(QLatin1Char('x')) / 2 - margin;
    To copy to clipboard, switch view to plain text mode 
    When the label's frameShape is set to StyledPanel, frameWidth returns 1.

Similar Threads

  1. how to set custom font to qlabel
    By binary001 in forum Newbie
    Replies: 2
    Last Post: 8th March 2015, 13:40
  2. auto resize font in QSpinBox
    By GoranShekerov in forum Qt Programming
    Replies: 3
    Last Post: 5th March 2015, 13:35
  3. Autoscaling widget font on resize
    By SeanM in forum Qt Programming
    Replies: 0
    Last Post: 10th March 2014, 17:27
  4. How to resize label font to fit size
    By P@u1 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2011, 00:07
  5. Replies: 1
    Last Post: 2nd August 2008, 16:46

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.