PDA

View Full Version : Prevent a QLabel resizing the parent QWidget



danc81
10th November 2009, 15:37
Hi,

This is probably a very basic question but I can't seem to find the correct way to make it work. I have a QWidget with a horizontal layout, in the layout among other controls is a QLabel. When the text of this label is longer than the widget of the widget, the parent widget is resized to accommodate the text. What I would like is the widget to remain the same size and the text just overflow.

I can set the parent QWidget size policy to 'Fixed' but the parent QWidget sits in another layout that can be resized.

So in essence, what I need to do is force the child QLabel width to never expand beyond the parent QWidget width.

How can I achieve that?

Thanks.

bdp32
10th November 2009, 17:10
I'm not sure of the "best way", but one a quick way would be to set the maximum height of the QLabel to the correct size to hold one line of text for the font used (look up QWidget::fontMetrics to help with this), then enable word wrapping.

This will give the effect you describe, for a nicer effect have a further look at QFontMetrics, specifically the elidedText() function.

Hope this helps.

squidge
10th November 2009, 17:44
When the text of this label is longer than the widget of the widget, the parent widgetSorry, thats just a widget overflow ;)

If your trying to do what I think your trying to do, why not just use the minimumSize and maximumSize properties?

danc81
10th November 2009, 17:49
I've tried setting maximum height and enabling word wrap but the QLabel still expands vertically when the text wraps.

Do you mean set minimumSize and maximumSize on the container widget? If so, that won't work as the container widget needs to resize according to it's parent (QScrollArea). I just need the QLabel not to overflow the parent QWidget but still allow the parent QWidget to adjust according to it's container QScrollArea.

squidge
10th November 2009, 21:35
So put the label in another container which can't resize?

danc81
10th November 2009, 21:38
I'm not sure that would solve anything, the QLabel needs to resize but not beyond the width of its parent. It's parent can resize to any width possible within the bounds of the screen, there is no fixed size to adhere to.

danc81
10th November 2009, 21:54
In the end I just resized the child QLabel to fit the parent widget in the resizeEvent(), works as expected but I just thought there must be some design concept I was missing somewhere to achieve it using the correct size properties.