Is it possible to use Elide with QLabel?

3

I want to use the element QLabel and if the text execest the width size it may look like this:

  • Add 3 points to the right:

    Foo bar baz foo bar...
    
  • Add 3 points from the center:

    Foo bar ba...oo bar baz
    
  • Add 3 points left:

    ...bar baz foo bar baz
    

Is it possible to do this natively, or do I have to reimplement using QFontMetrics with QResizeEvent ?

    
asked by anonymous 21.12.2016 / 13:06

2 answers

2

The response from Luiz helped me, however I needed something I did automatically, I thought I'd use paintEvent or something, but resizeEvent worked fine,

  • elidedlabel.h

    #ifndef ELIDEDLABEL_H
    #define ELIDEDLABEL_H
    
    #include <QLabel>
    
    class ElidedLabel : public QLabel
    {
        Q_OBJECT
    
    public:
        explicit ElidedLabel(QWidget *parent=0, Qt::WindowFlags f=0);
        explicit ElidedLabel(const QString &text, QWidget *parent=0, Qt::WindowFlags f=0);
        void setType(const Qt::TextElideMode type);
    
    public slots:
        void setText(const QString &text);
        void elide();
    
    protected:
        void resizeEvent(QResizeEvent *event);
    
    private:
        QString original;
        Qt::TextElideMode defaultType;
        bool eliding;
    
    };
    
    #endif // ELIDEDLABEL_H
    
  • elidedlabel.cpp

    #include "elidedlabel.h"
    
    #include <QResizeEvent>
    #include <QTimer>
    
    ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f) :
        QLabel(parent, f)
    {
        defaultType = Qt::ElideMiddle;
        eliding = false;
        original = "";
    }
    
    ElidedLabel::ElidedLabel(const QString &text, QWidget *parent, Qt::WindowFlags f) :
        QLabel(text, parent, f)
    {
        defaultType = Qt::ElideMiddle;
    
        //Usado para verificar se a string está ou não sendo atualizado
        eliding = false;
    
        //Guarda o texto original
        setText(text);
    }
    
    void ElidedLabel::setType(const Qt::TextElideMode type)
    {
        /*
           Altera o tipo de elide, podendo ser:
           Esquerda: "... bar baz"
           Meido: "Foo ... baz"
           Direita: "Foo bar ..."
        */
        defaultType = type;
        elide();
    }
    
    //Atualiza o texto se o Label for redimensionado
    void ElidedLabel::resizeEvent(QResizeEvent *event)
    {
        Q_UNUSED(event);
    
        //O delay é necessário para evitar conflitos
        QTimer::singleShot(50, this, SLOT(elide()));
    }
    
    //Atualiza o texto
    void ElidedLabel::setText(const QString &text)
    {
        original = text;
        QLabel::setText(text);
    
        //Executa no momento que é atualizado
        elide();
    }
    
    void ElidedLabel::elide()
    {
        if (eliding == false) {
            eliding = true;
    
            QFontMetrics metrics(font());
            QLabel::setText(metrics.elidedText(original, defaultType, width()));
    
            eliding = false;
        }
    }
    

To use just call it like this:

#include "elidedlabel.h";

...

ElidedLabel label1;

Or:

ElidedLabel *label1 = new ElidedLabel;
    
02.01.2017 / 15:04
6

There does not seem to be anything native. But you can use the QFontMetrics::elidedText method to implement an elided text of a line. Here's an example ( original source of the SOen sample ):

QString text("some long text without elipsis");
QFontMetrics metrics(label->font());
QString elidedText = metrics.elidedText(text, Qt::ElideRight, label->width());
label->setText(elidedText);

There is also a more complete example in the Qt documentation, where a new class specific to this is implemented to allow doing this in Labels with multiple lines. Oh, you can also use the%% Qxt extension class because it already implements what you you want the method QxtLabel .

    
21.12.2016 / 13:27