Reputation: 46
I need a Qt widget that displays plain text that scales automatically. This means when I resize the window that has this widget in its layout, font size is adjusted to the size of the widget to display the text in font as big as possible to fit in the size that layout dictates. Word wrap is a likely bonus.
I think, someone has already implemented such widget, but I couldn't google it.
Upvotes: 0
Views: 2052
Reputation: 2881
You could do it on the resize event of your window:
void MainWindow::resizeEvent(QResizeEvent*)
{
QFont f = label->font(); //Get label font
QFontMetrics metrics(f);
QSize size = metrics.size(0, label->text()); //Get size of text
float factorw = label->width() / (float)size.width(); //Get the width factor
float factorh = label->height() / (float)size.height(); //Get the height factor
float factor = qMin(factorw, factorh); //To fit contents in the screen select as factor
//the minimum factor between width and height
f.setPointSizeF(f.pointSizeF() * factor); //Set font size
label->setFont(f); //Set the adjusted font to the label
}
Upvotes: 2