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 required height for a QLabel without showing the label?
QtWS25 Last Chance

How to get required height for a QLabel without showing the label?

Scheduled Pinned Locked Moved Solved General and Desktop
qlabelqt for pythonqt for windows
10 Posts 4 Posters 5.1k 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.
  • T Offline
    T Offline
    ThePyGuy
    wrote on last edited by ThePyGuy
    #1

    Suppose I have a QLabel with some alignment and wordWrap as True, I have fixed width for it, Is there any way I can get the height required for this label without displaying the label?

    label = QLabel()
    label.setAlignment(Qt.AlignLeft)
    label.setWordWrap(True)
    label.setFixedWidth(60)
    label.setText('Analysis Reference Range Indicator (ANRIND)')
    

    I need to estimate the height required for a text with wrapping when width is given. I tried QFontMetrics() , it is able to give the height, width and the bounding rectangle, however I'm interested on height the text will require for a given width.

    I tried label.height() but it is always 480, I guess it's because it hasn't been shown yet.

    JoeCFDJ 1 Reply Last reply
    1
    • JonBJ JonB

      @ThePyGuy
      And I'm saying I believe there is not. This kind of question has been asked before, and that has always been the answer. And it has been what I have found in my code. Even more so with word wrapping, I can imagine Qt won't even calculate that until it really has to show the item. Unless it has some ability to output to a "hidden" or "null" device in memory where you could measure. But nobody has ever mentioned that Qt has such before. And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

      The obvious question is why you need to know this before it is shown? Because I don't think you're going to be able to get it.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #7

      @JonB said in How to get required height for a QLabel without showing the label?:

      And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

      QFontMetrix is not limited to a single line of text, here's an excellent example from SO:

      https://stackoverflow.com/a/37674690/15422846


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JonBJ T 2 Replies Last reply
      2
      • T ThePyGuy

        Suppose I have a QLabel with some alignment and wordWrap as True, I have fixed width for it, Is there any way I can get the height required for this label without displaying the label?

        label = QLabel()
        label.setAlignment(Qt.AlignLeft)
        label.setWordWrap(True)
        label.setFixedWidth(60)
        label.setText('Analysis Reference Range Indicator (ANRIND)')
        

        I need to estimate the height required for a text with wrapping when width is given. I tried QFontMetrics() , it is able to give the height, width and the bounding rectangle, however I'm interested on height the text will require for a given width.

        I tried label.height() but it is always 480, I guess it's because it hasn't been shown yet.

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        @ThePyGuy add this in your class.
        protected:
        void QWidget::resizeEvent(QResizeEvent *event)
        in this func you can find the height of your label for your estimation.

        T 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @ThePyGuy add this in your class.
          protected:
          void QWidget::resizeEvent(QResizeEvent *event)
          in this func you can find the height of your label for your estimation.

          T Offline
          T Offline
          ThePyGuy
          wrote on last edited by
          #3

          @JoeCFD It's a local variable, it's sole purpose is to estimate the height, it is neither added to any layout, not is it displayed to the user, so I don't think that it is going to work in the paintEvent, actually for this label, paintEvent isn't even going to call I guess, since it's locally created and deleted without displaying it.

          JonBJ 1 Reply Last reply
          0
          • T ThePyGuy

            @JoeCFD It's a local variable, it's sole purpose is to estimate the height, it is neither added to any layout, not is it displayed to the user, so I don't think that it is going to work in the paintEvent, actually for this label, paintEvent isn't even going to call I guess, since it's locally created and deleted without displaying it.

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

            @ThePyGuy
            I believe you are actually saying you want to know what the height will be when it word wraps. So far as I know, you cannot get the height till it is shown. Because it's not calculated till then. resizeEvent is not called till it's shown.

            T 1 Reply Last reply
            0
            • JonBJ JonB

              @ThePyGuy
              I believe you are actually saying you want to know what the height will be when it word wraps. So far as I know, you cannot get the height till it is shown. Because it's not calculated till then. resizeEvent is not called till it's shown.

              T Offline
              T Offline
              ThePyGuy
              wrote on last edited by
              #5

              @JonB Thanks for your response. That's what I'm asking, is there any way to get the required height without actually showing them?

              JonBJ 1 Reply Last reply
              0
              • T ThePyGuy

                @JonB Thanks for your response. That's what I'm asking, is there any way to get the required height without actually showing them?

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

                @ThePyGuy
                And I'm saying I believe there is not. This kind of question has been asked before, and that has always been the answer. And it has been what I have found in my code. Even more so with word wrapping, I can imagine Qt won't even calculate that until it really has to show the item. Unless it has some ability to output to a "hidden" or "null" device in memory where you could measure. But nobody has ever mentioned that Qt has such before. And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

                The obvious question is why you need to know this before it is shown? Because I don't think you're going to be able to get it.

                J.HilkJ T 2 Replies Last reply
                0
                • JonBJ JonB

                  @ThePyGuy
                  And I'm saying I believe there is not. This kind of question has been asked before, and that has always been the answer. And it has been what I have found in my code. Even more so with word wrapping, I can imagine Qt won't even calculate that until it really has to show the item. Unless it has some ability to output to a "hidden" or "null" device in memory where you could measure. But nobody has ever mentioned that Qt has such before. And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

                  The obvious question is why you need to know this before it is shown? Because I don't think you're going to be able to get it.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @JonB said in How to get required height for a QLabel without showing the label?:

                  And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

                  QFontMetrix is not limited to a single line of text, here's an excellent example from SO:

                  https://stackoverflow.com/a/37674690/15422846


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ T 2 Replies Last reply
                  2
                  • J.HilkJ J.Hilk

                    @JonB said in How to get required height for a QLabel without showing the label?:

                    And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

                    QFontMetrix is not limited to a single line of text, here's an excellent example from SO:

                    https://stackoverflow.com/a/37674690/15422846

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

                    @J-Hilk said in How to get required height for a QLabel without showing the label?:

                    QFontMetrix is not limited to a single line of text

                    I knew that, but I did not think it had access to where the Qt code would choose to do the word wrapping. However, from the link you reference I see

                    The rect argument for QFontMetrics::boundingRect constrains the layout of the input text. You can use Qt::TextWordWrap flag to wrap long lines to multiple rows within the constraint rect.

                    @ThePyGuy
                    That Qt::TextWordWrap flag could be just what you are looking for! Have a look at the accepted solution in that stackoverflow topic. A comment there states

                    No problem. If I add a QLabel label; label.setText(text); label.show(); qDebug() << label.rect(); it prints exactly the same rect as for the font metrics.

                    So that's indicating it should come up with the same answer as it will use when the label actually shown.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @ThePyGuy
                      And I'm saying I believe there is not. This kind of question has been asked before, and that has always been the answer. And it has been what I have found in my code. Even more so with word wrapping, I can imagine Qt won't even calculate that until it really has to show the item. Unless it has some ability to output to a "hidden" or "null" device in memory where you could measure. But nobody has ever mentioned that Qt has such before. And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

                      The obvious question is why you need to know this before it is shown? Because I don't think you're going to be able to get it.

                      T Offline
                      T Offline
                      ThePyGuy
                      wrote on last edited by
                      #9

                      @JonB I need it because I need to estimate the height required for a given text with wrapping, I'm trying to solve How to make QTableView to adjust the height of it's horizontal header automatically in PyQt? , One possible solution in my mind was to estimate the height for given width for the header text, and then to estimate the height accordingly.

                      1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @JonB said in How to get required height for a QLabel without showing the label?:

                        And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.

                        QFontMetrix is not limited to a single line of text, here's an excellent example from SO:

                        https://stackoverflow.com/a/37674690/15422846

                        T Offline
                        T Offline
                        ThePyGuy
                        wrote on last edited by
                        #10

                        @J-Hilk Thanks for the response, I'll look into that stackoverlfow thread

                        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