QLabel在QT中呈现文本时出现“文本溢出" [英] “text-overflow” for a QLabel’s text rendering in QT

查看:589
本文介绍了QLabel在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.

推荐答案

在您的标签上看起来像 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);

希望这会有所帮助

这篇关于QLabel在QT中呈现文本时出现“文本溢出"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆