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. How to get the MultiLine QLabel‘s text real height?
Forum Update on Monday, May 27th 2025

How to get the MultiLine QLabel‘s text real height?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 6.6k Views
  • 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.
  • L Offline
    L Offline
    ljc123456gogo
    wrote on last edited by ljc123456gogo
    #1

    Hi!
    I have a MultiLine QLabel ( use setWordWrap(multLine) ) . For some special reason, I must fix the QLabel's size. And I want to decrease the font size when the text real height heigher than QLabel's fixSize.(It means that adjust the FontSize to prevent the text which have long string to outside of label).
    I know to reimplement the resizeEvent , but I don't know how to get the MultiLine QLabel‘s text real height.
    Please give me some tips about how to get the MultiLine QLabel‘s text real height? Thanks in advance!

    Keep Hungery, Keep Foolish!

    kshegunovK 1 Reply Last reply
    0
    • L ljc123456gogo

      Hi!
      I have a MultiLine QLabel ( use setWordWrap(multLine) ) . For some special reason, I must fix the QLabel's size. And I want to decrease the font size when the text real height heigher than QLabel's fixSize.(It means that adjust the FontSize to prevent the text which have long string to outside of label).
      I know to reimplement the resizeEvent , but I don't know how to get the MultiLine QLabel‘s text real height.
      Please give me some tips about how to get the MultiLine QLabel‘s text real height? Thanks in advance!

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @ljc123456gogo
      Hello,
      Maybe the QFontMetrics class could be useful, especially QFontMetrics::boundingRect?

      Kind regards.

      Read and abide by the Qt Code of Conduct

      L 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @ljc123456gogo
        Hello,
        Maybe the QFontMetrics class could be useful, especially QFontMetrics::boundingRect?

        Kind regards.

        L Offline
        L Offline
        ljc123456gogo
        wrote on last edited by ljc123456gogo
        #3

        @kshegunov

        Hello. I had used the QFontMetrics like:

        QFont f(font);
        f.setPixelSize( fontSize );
        QRect TextRect = QFontMetrics(f).boundingRect( this->text() );
        

        But it only get the SingleLine QLabel's text height. (Because it just refer the string and hadn't involve real text?)
        Is it my use way wrong?

        Keep Hungery, Keep Foolish!

        kshegunovK 1 Reply Last reply
        0
        • L ljc123456gogo

          @kshegunov

          Hello. I had used the QFontMetrics like:

          QFont f(font);
          f.setPixelSize( fontSize );
          QRect TextRect = QFontMetrics(f).boundingRect( this->text() );
          

          But it only get the SingleLine QLabel's text height. (Because it just refer the string and hadn't involve real text?)
          Is it my use way wrong?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @ljc123456gogo
          That's because by default boundingRect will not expand the newlines (or tabs). Try something like this:

          static const int TabSize = 4;
          QFontMetrics metrics(font());
          QRect rect = metrics.boundingRect(QApplication::desktop()->geometry(), alignment() | Qt::TextWordWrap | Qt::TextExpandTabs, text(), TabSize);
          

          Note:
          If you don't have tabs, or don't wish to expand them, then don't pass Qt::TextExpandTabs as flag and TabSize as argument to the function.

          Note 2:
          If you in fact have a maximum rectangle that the text could occupy, pass that for the first argument instead of QApplication::desktop()->geometry(), e.g.:

          QRect rect = metrics.boundingRect(QRect(0, 0, maxWidth, maxHeight), alignment() | Qt::TextWordWrap | Qt::TextExpandTabs, text(), TabSize);
          // ... or the equivalent:
          QRect rect = metrics.boundingRect(0, 0, maxWidth, maxHeight, alignment() | Qt::TextWordWrap | Qt::TextExpandTabs, text(), TabSize);
          

          Kind regards.

          Read and abide by the Qt Code of Conduct

          L 1 Reply Last reply
          2
          • kshegunovK kshegunov

            @ljc123456gogo
            That's because by default boundingRect will not expand the newlines (or tabs). Try something like this:

            static const int TabSize = 4;
            QFontMetrics metrics(font());
            QRect rect = metrics.boundingRect(QApplication::desktop()->geometry(), alignment() | Qt::TextWordWrap | Qt::TextExpandTabs, text(), TabSize);
            

            Note:
            If you don't have tabs, or don't wish to expand them, then don't pass Qt::TextExpandTabs as flag and TabSize as argument to the function.

            Note 2:
            If you in fact have a maximum rectangle that the text could occupy, pass that for the first argument instead of QApplication::desktop()->geometry(), e.g.:

            QRect rect = metrics.boundingRect(QRect(0, 0, maxWidth, maxHeight), alignment() | Qt::TextWordWrap | Qt::TextExpandTabs, text(), TabSize);
            // ... or the equivalent:
            QRect rect = metrics.boundingRect(0, 0, maxWidth, maxHeight, alignment() | Qt::TextWordWrap | Qt::TextExpandTabs, text(), TabSize);
            

            Kind regards.

            L Offline
            L Offline
            ljc123456gogo
            wrote on last edited by
            #5

            @kshegunov
            It works! Thanks very much!

            Keep Hungery, Keep Foolish!

            kshegunovK 1 Reply Last reply
            0
            • L ljc123456gogo

              @kshegunov
              It works! Thanks very much!

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @ljc123456gogo
              No problem! If the solution works for you, please mark the thread as "solved", thank you.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              • Login

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