隐藏或裁剪QLabel中的重叠文本? [英] Hide or crop overlapping text in QLabel?

查看:330
本文介绍了隐藏或裁剪QLabel中的重叠文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个UI,其中确实有3个以水平布局排列的标签:

I'm currently building a UI where I do have 3 labels that are arranged in a horizontal layout:

| textLabel                    | valueLabel | unitLabel |

valueLabelunitLabel右对齐. unitLabel具有固定的宽度,valueLabel的宽度是可变的,并且取决于其中的文本长度. textLabel左对齐,并向右填充剩余的水平空间,直到valueLabel.

The valueLabel and unitLabel are aligned right. unitLabel has a fixed width, valueLabel's width is variable and depends on the text length inside it. textLabel is aligned left and fills the remaining horizontal space right up to the valueLabel.

因此,换句话说,textLabel的宽度不是固定的,而是取决于valueLabel的宽度.

So, in other words, the textLabel's width is not fixed but depends on the width of valueLabel.

我的问题:textLabel中的文本太长时,它会覆盖valueLabel.

My problem: when the text inside textLabel gets too long, it overlays the valueLabel.

有没有一种方法可以隐藏或切断重叠的文字?我想到了可以为<div>设置的CSS中的overflow: hidden属性之类的东西.我还尝试将QLineEdit用作工作方法,但似乎没有办法使QLineEdit背景透明.我该如何解决这个问题?预先感谢!

Ist there a way to hide or cut off the overlapping text? I think of something like the overflow: hidden attribute in CSS that you can set for a <div>, or something similar. I also tried to make use of a QLineEdit as a workaraound, but it seems that there is no way to make the QLineEdit background transparent. How can I resolve this issue? Thanks in advance!

推荐答案

布局中的小部件始终不能重叠,因此我看不到textLabel可以重叠valueLabel的任何方式.您的窗口小部件很可能不受布局管理,即使它们已添加到布局中也是如此.带有标签的布局可能不是其他布局的子级,或者未在容器窗口小部件上设置.

Widgets in a layout are always managed not to overlap, so I just see no way that the textLabel could overlap valueLabel. Most likely your widgets are not managed by the layout, even if they were added to the layout. Perhaps the layout with labels is not a child of another layout, or is not set on a container widget.

您没有告诉我们一些事情.一个独立的测试用例会很好.

You're not telling us something. A self-contained test case would be good to have.

如果您希望标签通过用"..."结束而不是突然切掉来使文本消失,则可以使用以下省略的样式.

If you want a label to elide the text by finishing it with "..." instead of abruptly cutting it off, the following elided style can be used.

// Usage:
/*
   QApplication app;
   app.setStyle(new ElidedStyle);
   ...
   QWidget * w = new QLabel("Hello World!");
   w->setProperty("elidedItemText", true); 
*/

// Interface

class ElidedStyle : public QProxyStyle
{
public:
   static QString elidedText(const QString & text, QPainter * painter, const QRect & rect);
   virtual void drawItemText(
      QPainter * painter, const QRect & rect, int flags, const QPalette & pal, 
      bool enabled, const QString & text, QPalette::ColorRole textRole = QPalette::NoRole) const Q_DECL_OVERRIDE;
};

// Implementation

QString ElidedStyle::elidedText(const QString & text, QPainter * painter, const QRect & rect)
{
   QWidget * widget = dynamic_cast<QWidget*>(painter->device());
   if (widget && widget->property("elidedItemText").toBool()) {
      QFontMetrics fm(painter->font());
      return fm.elidedText(text, Qt::ElideMiddle, rect.width());
   }
   return text;
}

void ElidedStyle::drawItemText(
   QPainter * painter, const QRect & rect, int flags, const QPalette & pal,
   bool enabled, const QString & text, QPalette::ColorRole textRole) const
{
   QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, elidedText(text, painter, rect), textRole);
}

这篇关于隐藏或裁剪QLabel中的重叠文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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