使 QLabel 即使截断文本也能缩小 [英] Enable QLabel to shrink even if it truncates text

查看:39
本文介绍了使 QLabel 即使截断文本也能缩小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调整 QLabel 的大小,即使这意味着截断其包含的文本?我有一个 QLabel 拉伸小部件的整个水平空间.在设置它的文本时,我确保它被正确截断,即获取它的 FontMetrics 和 Width 并使用 metrics.elidedText().

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().

但是当用户调整小部件的大小时,标签不允许它进一步缩小,因为它会截断其文本.

But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.

任何想法如何解决这个问题?我认为最简单的解决方案是以某种方式告诉 QLabel 始终缩小,然后捕获调整大小事件并正确设置文本格式 - 我只是不知道如何做第一部分(不同的大小策略无济于事)

Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)

推荐答案

虽然你提到设置尺寸策略没有帮助,但将 QLabel 的水平尺寸策略设置为 QSizePolicy::Ignored 应该告诉包含的布局管理器忽略标签中的任何最小尺寸提示.另一种方法是将 QLabel 的最小水平尺寸设置为非零值,例如 1.如果这些都不起作用,则有其他干扰.

Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

这篇关于使 QLabel 即使截断文本也能缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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