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 adjustSize
QtWS25 Last Chance

QLabel adjustSize

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 6.9k 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.
  • SPlattenS SPlatten

    According to:

    https://doc.qt.io/qt-5.9/qwidget.html#adjustSize

    Calling adjustSize, "adjusts the size of the widget to fit its contents.", however I have a QLabel, I set the font size in CSS before calling adjustSize, it does not take into account the font size and the geometry is not large enough to accommodate a larger font.

    I set the font size with:

        font-size:36pt;
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @SPlatten
    Possible suggestions:

    • adjustSize() cannot take into account QSS stylesheet, only Qt style & sizeHint() stuff?

    • Try the appropriate QWidget::ensurePolished()/polish(). I don't think it's this case, but when you do dynamic stylesheet stuff there are situations where you must do this to get Qt to properly take notice.

    • Test whether adjustSize() does get it right if you call it (e.g. from a QTimer) after the label has been allowed to actually render its newly-sized text?

    1 Reply Last reply
    1
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #3

      I did add a 1000 ms timer to call adjustSize and report the geometry in the debugger. The result was that the widget and geometry are now perfectly correct which suggests something else is not set before the initial adjustSize is called.

      Kind Regards,
      Sy

      JonBJ B 2 Replies Last reply
      0
      • SPlattenS SPlatten

        I did add a 1000 ms timer to call adjustSize and report the geometry in the debugger. The result was that the widget and geometry are now perfectly correct which suggests something else is not set before the initial adjustSize is called.

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

        @SPlatten
        In some shape or form, Qt does/can not calculate size till a widget is actually shown. Presumably hence the behaviour you see.

        1 Reply Last reply
        3
        • SPlattenS SPlatten

          I did add a 1000 ms timer to call adjustSize and report the geometry in the debugger. The result was that the widget and geometry are now perfectly correct which suggests something else is not set before the initial adjustSize is called.

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #5

          @SPlatten
          A text QLabel's size hint is not effective until showEvent.

          JonBJ 1 Reply Last reply
          3
          • SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #6

            @JonB, @Bonnie, thats good to know, I will have to factor that into my application as other widgets may be dependent on its size and position.

            Is there an event or signal that is fired when a widget is rendered?

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • B Bonnie

              @SPlatten
              A text QLabel's size hint is not effective until showEvent.

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

              @Bonnie said in QLabel adjustSize:

              A text QLabel's size hint is not effective until showEvent.

              But (I read it as --- or maybe this is not what @SPlatten meant?) the QLabel is already shown, but a stylesheet is now being set which will change its size. In which case, it won't get a new showEvent() to tell it when it's ready to adjustSize(), will it?

              B 1 Reply Last reply
              0
              • JonBJ JonB

                @Bonnie said in QLabel adjustSize:

                A text QLabel's size hint is not effective until showEvent.

                But (I read it as --- or maybe this is not what @SPlatten meant?) the QLabel is already shown, but a stylesheet is now being set which will change its size. In which case, it won't get a new showEvent() to tell it when it's ready to adjustSize(), will it?

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #8

                @JonB
                If you set the style sheet after showEvent, then, yes.
                I think the best way is reacting to it's parent widget's QEvent::LayoutRequest.

                JonBJ 1 Reply Last reply
                2
                • B Bonnie

                  @JonB
                  If you set the style sheet after showEvent, then, yes.
                  I think the best way is reacting to it's parent widget's QEvent::LayoutRequest.

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

                  @Bonnie said in QLabel adjustSize:

                  I think the best way is reacting to it's parent widget's QEvent::LayoutRequest.

                  Is there anything you don't know? ;-)

                  B 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Bonnie said in QLabel adjustSize:

                    I think the best way is reacting to it's parent widget's QEvent::LayoutRequest.

                    Is there anything you don't know? ;-)

                    B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by
                    #10

                    @JonB
                    Of course! A lot!
                    I did spend quite some time looking into the size hint changing thing of QLabel though, it's quite annoying.

                    1 Reply Last reply
                    1
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #11

                      After revisiting my work and redesigning my class, with the CSS set before the text and geometry, the adjustSize() works correctly and does take into account the font size.

                      Kind Regards,
                      Sy

                      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