Rajeshwar
Rajeshwar

Reputation: 11651

Prevent a QLabel from expanding horizontally with large words

I currently have something like this

QLabel* l = new QLabel(this);
l->setTextFormat(Qt::RichText);
l->set_IsSelf(IsSelf);
l->setWordWrap(true);
l->setText("Thissssssssssssssssssssssssssssssss"); 
l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
l->setMaximumWidth(40);

Now I realize that width is very small and thats ok. What I want is to display all the content and make it expand vertically.

Upvotes: 1

Views: 1733

Answers (1)

Nejat
Nejat

Reputation: 32635

You should insert your label in a layout which it's sizeconstraint is set to QLayout::SetMinimumSize and set the vertical sizepolicy of your label to QSizePolicy::MinimumExpanding :

QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);

QLabel* l = new QLabel;
l->setWordWrap(true);
l->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);

layout->addWidget(l);

Upvotes: 1

Related Questions