19

Using the designer in Qt creator I have created a dialog that contains various widgets in a vertical layout. One of the widgets is a QLabel with word wrap set to true. The text for the QLabel is set just before the dialog is shown.

The maximum width and height of the QLabel is 16777215, the vertical size policy is set to Expanding and the horizontal is Preferred. I've tried changing changing the size policy.

The problem I have is that if the text is large, the QLabel fails to be adjusted accordingly and the text is clipped, like this: -

enter image description here

I have tried calling updateGeometry() for the dialog, after setting the text and also tried calling update on the vertical layout, but nothing appears to make any difference. Ideally, I want the QLabel to adjust vertically to accommodate the text.

Can someone tell me what I'm missing here?

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85

2 Answers2

25

Set the vertical sizepolicy of your label to QSizePolicy::Minimum. Then set the sizeconstraint of your dialog's layout to QLayout::SetMinimumSize. This should make your dialog grow so all the content will fit inside of it.

Something like this:

QVBoxLayout *layout = new QVBoxLayout;
layout->setSizeConstraint(QLayout::SetMinimumSize);
this->setLayout(layout);
for(int i = 0; i < 5; i++)
{
    QLabel *label = new QLabel;
    label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
    label->setWordWrap(true);
    label->setText("This is a very long text. This is a very long text. This is a very long text. "
                   "This is a very long text. This is a very long text. This is a very long text. This is a very long text. "
                   "This is a very long text. This is a very long text.");
    layout->addWidget(label);
}
thuga
  • 12,601
  • 42
  • 52
  • 1
    Thanks, but this doesn't work. Now the text takes up just two lines and the rest of the text is truncated; something else must be going on here. – TheDarkKnight Oct 10 '13 at 12:49
  • @Merlin069 That's odd. The example should work though. Can you produce an SSCE? – thuga Oct 10 '13 at 12:59
  • 1
    Ok, I finally have it working; setting the label's size policy to QSizePolicy::MinimumExpanding did it, though I'm not sure why. I would have also expected QSizePolicy::Minimum to work. Thanks for the help. – TheDarkKnight Oct 10 '13 at 13:18
  • @Merlin069 Oh. Good to know. That is strange. – thuga Oct 10 '13 at 13:24
  • 1
    @Merlin069, please update your answer as for when someone is looking for the solution to just check your answer without the need to go to the comments. :) – Zdravko Donev Jan 25 '17 at 09:10
4

In my experiments, just setting the layoutSizeConstraint property to SetMinimumSize on the layout that contains the QLabel should be enough to let the label expand and adjust to its contents.

You can either change that property in Qt Designer, if you used it to build your UI, or via code:

layout->setSizeConstraint(QLayout::SetMinimumSize);

Note that if you have nested layouts, you may need to set the constraint in all layouts up the chain. No changes to the label's own sizePolicy is needed -- the defaults (Preferred for both the horizontal and vertical size policy) should work, at least in my experience.

waldyrious
  • 3,683
  • 4
  • 33
  • 41