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. Having QLabels resize outside a layout
Qt 6.11 is out! See what's new in the release blog

Having QLabels resize outside a layout

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 867 Views 1 Watching
  • 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.
  • C Offline
    C Offline
    calawap
    wrote on last edited by
    #1

    I have some QLabels which I don't want to put in a layout, so I can set their positions pixel-by-pixel. They have some text, which I want to be able to change later.

    Whenever I call setText() on one of them after-the-fact, changing the contents, the width of the label stays the same as it was for the old string. I have to call resize() and update() to update the size. However, I have to either guess at this new size or calculate it based on the font metrics, which is far from ideal.

    If I put the QLabels in a layout, they resize automatically when I call setText(). Is there a way to make them do that from outside a layout?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goblincoding
      wrote on last edited by
      #2

      I am not sure if this will help you, but I had a similar problem with QLabels regarding move() operations. What worked for me was to show() them after setText() (even if they were already visible).

      I am not 100% sure, but I believe it might be related to the following (from the QWidget documentation for visible()):

      "...If its size or position has changed, Qt guarantees that a widget gets move and resize events just before it is shown. If the widget has not been resized yet, Qt will adjust the widget's size to a useful default using adjustSize()..."

      http://www.goblincoding.com

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi,
        Since you place them outside a layout, you, and you alone, are responsible for resize operations. How should Qt be able to do so if it doesn't know how to resize (layout operation). Maybe with the textChanged signal do a calculation based on the font and string or call the resize and update.
        Greetz

        Greetz, Jeroen

        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