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 Fixed count of Rows
Qt 6.11 is out! See what's new in the release blog

QLabel Fixed count of Rows

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.2k 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I want to have a Qt Label with a fixed size which can display n lines of text.

    My hacky solution for this was this (with n = 4):

        QFontMetrics fm{ mAnnouncementLabel->font() };
        mAnnouncementLabel->setFixedHeight(fm.horizontalAdvance("Test") * 4);
    

    So what is the correct way to limit a QLabel to n lines of text?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • S sandro4912

      I want to have a Qt Label with a fixed size which can display n lines of text.

      My hacky solution for this was this (with n = 4):

          QFontMetrics fm{ mAnnouncementLabel->font() };
          mAnnouncementLabel->setFixedHeight(fm.horizontalAdvance("Test") * 4);
      

      So what is the correct way to limit a QLabel to n lines of text?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @sandro4912 Depending on what you want to do you could use one QLabel for each line :-)

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S sandro4912

        I want to have a Qt Label with a fixed size which can display n lines of text.

        My hacky solution for this was this (with n = 4):

            QFontMetrics fm{ mAnnouncementLabel->font() };
            mAnnouncementLabel->setFixedHeight(fm.horizontalAdvance("Test") * 4);
        

        So what is the correct way to limit a QLabel to n lines of text?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @sandro4912 said in QLabel Fixed count of Rows:

        horizontalAdvance

        QFontMetrics::horizontalAdvance() looks like it returns some horizontal distance? Presumably you rather want something which measures the font vertically, and (I don't know) includes the inter-line gap, if applicable? I'm thinking QFontMetrics::lineSpacing() * 4?

        S 1 Reply Last reply
        2
        • JonBJ JonB

          @sandro4912 said in QLabel Fixed count of Rows:

          horizontalAdvance

          QFontMetrics::horizontalAdvance() looks like it returns some horizontal distance? Presumably you rather want something which measures the font vertically, and (I don't know) includes the inter-line gap, if applicable? I'm thinking QFontMetrics::lineSpacing() * 4?

          S Offline
          S Offline
          sandro4912
          wrote on last edited by
          #4

          @JonB That did the job now the label height looks correct for 4 lines.

          @jsulm I thought several Labels is a bit overkill. For now i build a string together with \n at the end depending on how many messages show up (Maximum can be 4). I guess I could also pack the QLabel in a QVector and display / fill that.

          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