Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel with resizedText
Qt 6.11 is out! See what's new in the release blog

QLabel with resizedText

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 384 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    Hi,

    I would like to have a subclass of QLabel, where the text's width will have a 50% of QLabel's width:

    text.png

    my resizeEvent:

    int textWidth = fontMetrics().horizontalAdvance(text());
    

    And here is the problem: I know that I have to use create QFont variable, setPixelSize() and set this font in QLabel like this: setFont(font).
    But how can I calculate textWidth to setPixelSize?

    mrjjM 1 Reply Last reply
    0
    • T TomNow99

      Hi,

      I would like to have a subclass of QLabel, where the text's width will have a 50% of QLabel's width:

      text.png

      my resizeEvent:

      int textWidth = fontMetrics().horizontalAdvance(text());
      

      And here is the problem: I know that I have to use create QFont variable, setPixelSize() and set this font in QLabel like this: setFont(font).
      But how can I calculate textWidth to setPixelSize?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @TomNow99

      • But how can I calculate textWidth to setPixelSize?

      Normally you would simply reduce point size until your text fits the area.

      something like this. all credits to @SebastienL

      void scalePainterFontSizeToFit(QPainter &painter, QFont &r_font, float _heightToFitIn)
      {
          float oldFontSize, newFontSize, oldHeight;
      
          // Init
          oldFontSize=r_font.pointSizeF();
      
          // Loop
          for (int i=0 ; i<3 ; i++)
          {
              oldHeight = painter.fontMetrics().boundingRect('D').height();
              newFontSize = (_heightToFitIn / oldHeight) * oldFontSize;
              r_font.setPointSizeF(newFontSize);
              painter.setFont(r_font);
              oldFontSize = newFontSize;
              //qDebug() << "OldFontSize=" << oldFontSize << "HtoFitIn=" << _heightToFitIn << "  fontHeight=" << oldHeight << "  newFontSize=" << newFontSize;
          }
      
          // End
          r_font.setPointSizeF(newFontSize);
          painter.setFont(r_font);
      }
      

      from
      https://forum.qt.io/topic/28131/how-to-calcuate-font-size-to-fit-a-text-inside-a-given-rectangle/13

      Disclaimer. Not tested how well this code works. Its was more for the concept overall.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved