You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, March 20th 2010, 9:32pm

Dynamic font size in QLabel based on text length

Hi everyone.

I have a QLabel which I dynamically fill with text. Sometimes the text I need to show in the QLabel is very long and gets cut-off.

Is there any way I can configure the QLabel to use the maximum font size for the input text such that the text fits in the QLabel without overflowing ?

I'm using qt-embedded-linux-opensource-src-4.4.0.

Thanks,
Mark.

Junior

Professional

  • "Junior" is male

Posts: 1,623

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Saturday, March 20th 2010, 11:02pm

Mark,

Would this be something useful other than changing the font dynamically?

Source code

1
2
3
4
5
6
7
8
9
#include <QFontMetrics>
QString str = "Some long text entered"; // or add more to test with your label
QFontMetrics fm( label->font() );
if( label->width() < fm.width( str ) ){
  label->setToolTip( str );
  label->setText( label->fontMetrics().elidedText( str, Qt::ElideRight, label->width() ));
} else {
  label->setText( str );
}

If the text doesn't fit in the label, then the tooltip gets set and the text fills until the ... needs to be added to the end of the string within the label.
This is different approach to your question, just thought I would toss the idea at you and see if it was something that might work for you in lieu of the dynamic setting.

3

Sunday, March 21st 2010, 3:07am

Hi Junior, thanks for the reply.

Afraid not - the QT App I'm working with is displaying text on a TV screen with no user input/output - so they need to be able to see the full text upfront.

BUT, I haven't seen the QFontMetrics object before. Perhaps I could setup a loop which reduces the font size from a maximum until the width given by the font metric is less than the width of the label. At the moment I also have word-wrap on which helps. Unsure how QFontMetric handles word wrap, but its something for me to look into. Thanks !

4

Sunday, March 21st 2010, 4:00am

I'm interested in seeing how this works out. I use QFontMetrics extensively, but I've never gotten width() to be 100% accurate. I'm sure it has to do w/ each individual system and/or the QWidget being painted (or me just sucking). I've always found using a hard-coded "padding" value to save me. On a TV, you're going to have a lot less freedom though I would assume.

5

Monday, March 22nd 2010, 4:34am

width() on the FontMetrics didn't support word wrap, and I have 3 lines available for text to wrap into.

SO, I've ended up with this code below. It makes me sad, but it appears to work. If anyone knows of a better way I would love to hear it.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	int fit = false;
	this->testLabel->setText("");
	
	QLabel *myLabel = this->testLabel;
	QFont myFont = myLabel->font();	

	QString str = "this is the new string. this is the new string. this is the new string. super long. don't fit. please please please overflow the box";
	
	while (!fit)
	{
		QFontMetrics fm( myFont );		
		QRect bound = fm.boundingRect(0,0, myLabel->width(), myLabel->height(), Qt::TextWordWrap | Qt::AlignLeft, str);
		
		if (bound.width() <= myLabel->width() &&
		    bound.height() <= myLabel->height())
		    fit = true;
		else
			myFont.setPointSize(myFont.pointSize() - 1);
	}
	
	myLabel->setFont(myFont);
	myLabel->setText(str);