“text-overflow” for a QLabel’s text rendering in QT
Asked Answered
I

4

27

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.

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.

Implosive answered 11/9, 2011 at 20:40 Comment(0)
A
55

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);

hope this helps, regards

Audry answered 11/9, 2011 at 23:57 Comment(1)
Also consider reimplementing minimumSizeHint() with something like return QSize(0, QLabel::minimumSizeHint().height());. Otherwise the label cannot be resized below the original text width.Periodic
E
9

I've modified solution described above and created a function:

static void SetTextToLabel(QLabel *label, QString text)
{
    QFontMetrics metrix(label->font());
    int width = label->width() - 2;
    QString clippedText = metrix.elidedText(text, Qt::ElideRight, width);
    label->setText(clippedText);
}

Hope it will be useful.

Eyesore answered 28/2, 2012 at 8:11 Comment(1)
actually there's almost no difference, but subtracting some width does help a bitTrocki
P
2

Qt-5 includes an example of an elided label class which may be a useful reference when implementing your own.

From the example:

elidedlabel.h:

class ElidedLabel : public QFrame
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText)
    Q_PROPERTY(bool isElided READ isElided)

public:
    explicit ElidedLabel(const QString &text, QWidget *parent = 0);

    void setText(const QString &text);
    const QString & text() const { return content; }
    bool isElided() const { return elided; }

protected:
    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;

signals:
    void elisionChanged(bool elided);

private:
    bool elided;
    QString content;
};

elidedlabel.cpp:

ElidedLabel::ElidedLabel(const QString &text, QWidget *parent)
    : QFrame(parent)
    , elided(false)
    , content(text)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
}

void ElidedLabel::setText(const QString &newText)
{
    content = newText;
    update();
}

void ElidedLabel::paintEvent(QPaintEvent *event)
{
    QFrame::paintEvent(event);

    QPainter painter(this);
    QFontMetrics fontMetrics = painter.fontMetrics();

    bool didElide = false;
    int lineSpacing = fontMetrics.lineSpacing();
    int y = 0;

    QTextLayout textLayout(content, painter.font());
    textLayout.beginLayout();
    forever {
        QTextLine line = textLayout.createLine();

        if (!line.isValid())
            break;

        line.setLineWidth(width());
        int nextLineY = y + lineSpacing;

        if (height() >= nextLineY + lineSpacing) {
            line.draw(&painter, QPoint(0, y));
            y = nextLineY;
            //! [2]
            //! [3]
        } else {
            QString lastLine = content.mid(line.textStart());
            QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width());
            painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine);
            line = textLayout.createLine();
            didElide = line.isValid();
            break;
        }
    }
    textLayout.endLayout();

    if (didElide != elided) {
        elided = didElide;
        emit elisionChanged(didElide);
    }
}
Periodic answered 20/6, 2017 at 11:13 Comment(0)
T
1

This is how I achive. Behave just like regular QLabel yet with ellipsis.

class EllipsisLabel : public QLabel
{
    Q_OBJECT
public:
    explicit EllipsisLabel(QWidget *parent = nullptr);
    explicit EllipsisLabel(QString text, QWidget *parent = nullptr);
    void setText(QString);

protected:
    void resizeEvent(QResizeEvent *);

private:
    void updateText();
    QString m_text;

};
EllipsisLabel::EllipsisLabel(QWidget *parent)
    : EllipsisLabel("", parent)
{
}

EllipsisLabel::EllipsisLabel(QString text, QWidget *parent)
    : QLabel(parent)
{
    setText(text);
}

void EllipsisLabel::setText(QString text)
{
    m_text = text;
    updateText();
}

void EllipsisLabel::resizeEvent(QResizeEvent *event)
{
    QLabel::resizeEvent(event);
    updateText();
}

void EllipsisLabel::updateText()
{
    QFontMetrics metrics(font());
    QString elided = metrics.elidedText(m_text, Qt::ElideRight, width());
    QLabel::setText(elided);
}
Touched answered 11/8, 2022 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.