QT 中 QLabel 文本渲染的“文本溢出" [英] “text-overflow” for a QLabel’s text rendering in QT
问题描述
我在一个可以调整大小的小部件中有一个 QLabel 元素.文本可能会溢出边界,所以我需要让应用程序看起来更优雅,某种方式使文本在文本中最后一个完全可见的单词之后生成一个省略号 (...).
I have got a QLabel element 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.
在 HTML/CSS 中制作布局我曾经使用 text-overflow: ellipsis;
来实现这一点,但对于 QT 类,我没有找到任何相关信息.
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.
推荐答案
它看起来像你的标签 resize event 您可以使用小部件的新宽度创建隐藏的文本并重置文本.使用 QFontMetrics::elidedText 方法获取省略的版本的字符串.
It looks like on your label 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);
希望能帮到你,问候
这篇关于QT 中 QLabel 文本渲染的“文本溢出"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!