Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“text-overflow” for a QLabel’s text rendering

I have got a QLabel in a widget which can be resized. The text can overflow boundaries, so I need, for the application to look more elegant, some way to make the text generate an ellipsis (...) after the last totally visible word in the text.

Making layouts in HTML/CSS I used to use text-overflow: ellipsis; for this, but for Qt classes, I have not found any information on this.


1 Answers

It looks like in your label's resize event, you can create elided text using the new width of the widget, and reset the text. Use QFontMetrics::elidedText method to get the elided version of the string.

QString text("some long text without elipsis");
QFontMetrics metrics(label->font());
QString elidedText = metrics.elidedText(text, Qt::ElideRight, label->width());
label->setText(elidedText);
like image 142
serge_gubenko Avatar answered Sep 10 '25 07:09

serge_gubenko