Results 1 to 13 of 13

Thread: Auto resize QLabel

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Auto resize QLabel

    From Qt docs

    The label resizes itself if auto-resizing is enabled.
    But I am not able to figure how to enable auto-resizing.

    Can someone please help?

    Thanks a lot.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Auto resize QLabel

    QWidget::adjustSize() does the trick, although you have to call it manually everytime you change contents of a label which is not in a layout.

    And what comes to auto-resize, it's marked obsolete already in qt3's qlabel docs. I still wonder what does the above-mentioned sentence do in all over qt4's qlabel docs.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize QLabel

    although you have to call it manually everytime you change contents of a label which is not in a layout.
    How can I resize the label which is inside a layout ?

    Thanks a lot.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Auto resize QLabel

    Hmm, QLabel should update it's size hint to correspond new contents when setting text/pixmap/..

    If you are doing something custom, you could override QLabel::sizeHint() and return appropriate size for the widget (or maybe just set minimumSize). You may call QWidget::updateGeometry() to notify the layout system about the geometry change.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize QLabel

    will addStretch(1) do the trick ? I will probably try it and post the result.

    Thanks a lot

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Auto resize QLabel

    QBoxLayout::addStretch() will just add empty stretchable space to the end of the box layout. I don't know how the labels are even laid or what's the actual problem here, bu if you call addStretch() always when changing the pixmap of a label, you'll end up having as many spacers in the end of the layout..
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize QLabel

    Ok Let me explain.

    I have a two labels in a horizontal layout. The first label changes text (the new text is smaller in the length then the older text) on an event and therefore it should adjust its width so that the second label is next to it.

    Now, in the horizontal layout at the end I add stretch.

    My doubt is, will this stretch resize the label automatically ?

    Since I am not reimplementing QLabel how can I achieve this resizing?

    Thanks a lot.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize QLabel

    What exactly do you mean by "resizing a label"? You mean its content or the widget itself? Because if you only mean the widget, just set its size policy to expanding and the layout will take care of the rest. If you mean the content, then make sure scaledContents property of the label is set to true. That should suffice.

  9. #9
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Auto resize QLabel

    Sorry for digging this old thread but I have the same issue.

    I'm trying to write my own Message box class and I need a label to be resized when anybody changes it's content. The label is in a layout but when I set text to the label it doesn't resize the dialog
    C++ & AMD forever

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize QLabel

    Set a layout contstraint on the dialog's layout.

  11. #11
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Auto resize QLabel

    But which layoutConstraint should it be?
    C++ & AMD forever

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize QLabel

    FixedSize, I guess. I'm not really sure what you are doing, so it's just a guess.

  13. The following user says thank you to wysota for this useful post:

    THRESHE (6th August 2008)

  14. #13
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Auto resize QLabel

    Quote Originally Posted by wysota View Post
    FixedSize, I guess. I'm not really sure what you are doing, so it's just a guess.
    That helped Thanks
    C++ & AMD forever

Similar Threads

  1. [SOLVED] subclassing qlabel
    By mickey in forum Newbie
    Replies: 10
    Last Post: 4th June 2008, 15:43
  2. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 20:57
  3. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 01:32
  4. QLabel with HTML-style formatting docs?
    By Everall in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2006, 21:01
  5. How to get a QDockWidget to auto resize?
    By bitChanger in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2006, 17:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.