PDA

View Full Version : Resizing labels after setText()



stephelton
11th January 2013, 04:09
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?

Santosh Reddy
11th January 2013, 08:02
Are you using any layouts? If not you could use QGridLayout

stephelton
11th January 2013, 18:07
I'm using GridLayout. How would that help me?

Boron
11th January 2013, 20:13
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!

wysota
11th January 2013, 21:22
This example only requires one layout, not two.

stephelton
11th January 2013, 21:58
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.

wysota
11th January 2013, 22:06
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?

stephelton
12th January 2013, 01:16
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.

wysota
12th January 2013, 10:35
The parent sits in the window so the window IS the problem. Please answer the question.

stephelton
12th January 2013, 19:08
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...

Boron
12th January 2013, 20:09
Maybe you could provide a ui file, if you didn't code the GUI manually?

stephelton
12th January 2013, 20:16
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).

Boron
12th January 2013, 20:26
You should check the sizePolicy of the QLabels.
"Preferred" is the default value, which should be OK for these kinds of labels.

stephelton
12th January 2013, 21:01
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...

stephelton
12th January 2013, 23:17
SOLVED:

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

wysota
13th January 2013, 12:41
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.