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
Forum Update on Tuesday, May 27th 2025

QLabel Fixed count of Rows

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 957 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 4 Dec 2019, 08:55 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?

    J J 2 Replies Last reply 4 Dec 2019, 09:02
    0
    • S sandro4912
      4 Dec 2019, 08:55

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Dec 2019, 09:02 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
        4 Dec 2019, 08:55

        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?

        J Offline
        J Offline
        JonB
        wrote on 4 Dec 2019, 09:06 last edited by JonB 12 Apr 2019, 09:07
        #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 4 Dec 2019, 09:45
        2
        • J JonB
          4 Dec 2019, 09:06

          @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 4 Dec 2019, 09:45 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

          1/4

          4 Dec 2019, 08:55

          • Login

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