Results 1 to 16 of 16

Thread: Resizing labels after setText()

  1. #1
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Resizing labels after setText()

    I have a grid with a bunch of label: value pairs. As an example, it looks something like this:

    Total: 18
    Apples: 6 (33.33%)
    Oranges: 12 (66.66%)

    The labels on the left are column 0, the values on the right are column 1.

    The values change frequently with a call to setText(). When the length of their text glows large, they are truncated because the parent does not grow to accommodate them.

    What is the most efficient way to resize them as I change their text?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Resizing labels after setText()

    Are you using any layouts? If not you could use QGridLayout
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    I'm using GridLayout. How would that help me?

  4. #4
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing labels after setText()

    Your parent widget also needs a layout.

    Have a look at the attached example.ui.
    The six labels are arranged in a gridLayout, as you already have done.
    The gridLayout and the buttonBox are arranged in a VBoxLayout (V = vertical) by inside the dialog window.

    The VBoxLayout is not only arranged "around" the gridLayout and the buttonBox.
    The layout was applied to the QDialog itself!
    Attached Files Attached Files

  5. #5
    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: Resizing labels after setText()

    This example only requires one layout, not two.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    I don't think my layout is the problem. The whole thing looks just fine. I need to know the most efficient way I can cause the whole widget tree to resize as necessary when the size of the rendered text changes.

  7. #7
    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: Resizing labels after setText()

    Does your window still look fine if you change its size? Does all the text fit the window if you reduce the window size as much as you can?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    The window isn't the problem... it's the immediate parent, which doesn't resize. The text no longer fits in that parent when the length of the text increases.

  9. #9
    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: Resizing labels after setText()

    The parent sits in the window so the window IS the problem. Please answer the question.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    Quote Originally Posted by wysota View Post
    Does your window still look fine if you change its size? Does all the text fit the window if you reduce the window size as much as you can?
    Min window size is way too large to have anything to do with this. The text doesn't fit inside its parent regardless of the window size.

    I'm surprised this question hasn't gotten a quick answer. Most UI frameworks I've worked with (including one I wrote) use a means of invalidation to travel up the tree of widgets until the layout of all widgets can be accommodated. Take my ui, for example:

    - window
    - - tab panel
    - - - tab 1 (split panel)
    - - - - <bunch of stuff, makes the min window size quite large>
    - - - tab 2 (QWidget with default layout)
    - - - - QGroupBox (QHBoxLayout)
    - - - - - QGroupBox (GridLayout)
    - - - - - - QLabel*
    - - - - - - <Other QLabels>
    - - - - - <Other QGroupBox siblings>

    * denotes the problematic label. I change the label and it becomes too large for the space it has. In other systems I've worked with, here I'd invalidate either the label itself or its immediate parent. The parent would try to give it more space within the space it has. If it can, it stops there. If it can't, it goes up the tree to its parent, who attempts the same thing. And so on until either someone can conjure up more space or you get to the root widget.

    Does QT use something similar? If so, I think that's what I need here...

  11. #11
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing labels after setText()

    Maybe you could provide a ui file, if you didn't code the GUI manually?

  12. #12
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    It's all manual. A screen shot might go a long way... here you go:

    http://stephenashelton.com/ss/ui.png
    http://stephenashelton.com/ss/ui2.png

    As you can see in the first (ui.png) each box group there has a "Success: 1234 (100.00%)" label. This is the one that is changed frequently, and as the number there grows, it no longer has space. This is what I'm trying to fix.

    The other image is there to illustrate what makes the window so large (and irrelevant).

  13. #13
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing labels after setText()

    You should check the sizePolicy of the QLabels.
    "Preferred" is the default value, which should be OK for these kinds of labels.

  14. #14
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    Quote Originally Posted by Boron View Post
    You should check the sizePolicy of the QLabels.
    "Preferred" is the default value, which should be OK for these kinds of labels.
    I don't think it has anything to do with that. Even if I manually calculate the text width (via QFontMetrics) and call setMinimumWidth(), it still doesn't work.

    The problem seems to be that nothing is triggering the box to be laid out again.


    Added after 9 minutes:


    Another note -- if I delete the widget and add it back to the grid, it sizes correctly. I may have mentioned that already.

    Surely there's a much more efficient way of doing this, though...
    Last edited by stephelton; 12th January 2013 at 20:01.

  15. #15
    Join Date
    Jan 2013
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Resizing labels after setText()

    SOLVED:

    I just needed to call layout()->update().

  16. #16
    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: Resizing labels after setText()

    You should never need to call this method. If it works when you do then the problem is still there, you just masked it by calling some function that has a side effect which works around the real problem. But it might suddenly stop working some day without any apparent reason.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. settext
    By seltra in forum Newbie
    Replies: 4
    Last Post: 3rd October 2010, 14:52
  2. label setText value is 0
    By babygal in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2010, 07:58
  3. Replies: 3
    Last Post: 2nd March 2010, 20:58
  4. setText translation
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2009, 15:59
  5. QLabel::setText() how to
    By freak in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2006, 17:19

Tags for this Thread

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.