Hide or crop overlapping text in QLabel?
Asked Answered
N

1

0

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

| textLabel                    | valueLabel | unitLabel |

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.

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

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

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!

Nofretete answered 22/1, 2014 at 13:48 Comment(1)
I created a grid/horizontal layout and added "Horizontal Policy = ignored" to the attributes of the leftmost label. It does not seem to overlap, instead, once the leftmost label (textLabel) gets too long, it gets hidden under the middle label (valueLabel).Midget
V
2

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);
}
Ventriloquy answered 22/1, 2014 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.