I have a custom QLabel with wordwrap enabled. When resizing MyWidget, it wraps but the sizeHint() still returns the original height. I tried the fix from this post: QLabel cutting off text on resize but the sizeHint() of the label still returns the initial height and results in cutoff text.
What do I have to do in order to get the TestLabel to return the correct sizeHint()?
MyWidget::MyWidget(QWidget *parent)
: QFrame(parent)
{
label = new TestLabel(this);
label ->setWordWrap(true);
label ->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
mLayout->addWidget(label);
}
void MyWidget::resizeEvent( QResizeEvent * event )
{
int height = label->heightForWidth(label->width());
label->setMaximumHeight(height);
//label->setMinimumHeight(height); // gets called all the time
label->updateGeometry();
QFrame::resizeEvent(event);
}
the sizeHint() of the TestLabel class:
QSize TestLabel::sizeHint() const
{
QSize s = QLabel::sizeHint();
qDebug() << "sizeHint(): " << text() << ": " << s;
return s;
}
QSize TestLabel::minimumSizeHint() const
{
QSize s = QLabel::sizeHint();
qDebug() << "minimumSizeHint(): " << text() << ": " << s;
return s;
}
sizeHint()
is an indicator to the layout system as to how much space the widget would like to use if it had all the space in the world. Whether or not word wrapping is enabled has no effect on this, it just changes the behavior of theQLabel
when it doesn't get the size it "asked" the layout for. – Thayne