PDA

View Full Version : Auto resize QLabel



munna
10th June 2006, 12:14
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.

jpn
10th June 2006, 12:47
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.

munna
10th June 2006, 12:51
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.

jpn
10th June 2006, 14:20
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.

munna
10th June 2006, 14:28
will addStretch(1) do the trick ? I will probably try it and post the result.

Thanks a lot

jpn
10th June 2006, 14:36
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.. ;)

munna
10th June 2006, 14:43
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.

wysota
10th June 2006, 17:28
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.

THRESHE
5th August 2008, 16:09
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 :confused:

wysota
5th August 2008, 18:25
Set a layout contstraint on the dialog's layout.

THRESHE
6th August 2008, 10:12
But which layoutConstraint should it be?

wysota
6th August 2008, 12:38
FixedSize, I guess. I'm not really sure what you are doing, so it's just a guess.

THRESHE
6th August 2008, 13:39
FixedSize, I guess. I'm not really sure what you are doing, so it's just a guess.
That helped :D Thanks