Is it possible to use Elide with Qlabel?

Asked

Viewed 93 times

3

I’m trying to use the element QLabel and if the text execrates the width size it can look like this:

  • Add 3 points to the right:

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

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

    ...bar baz foo bar baz
    

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

2 answers

6

There seems to be nothing native. But you can use the method QFontMetrics::elidedText to implement an "elided text" of a line. Here is an example (original source for example in Soen):

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 this to be done in multi-line Labels. Ah, you can also use the class QxtLabel of the Qxt extension, because it already implements what you want in the method QxtLabel::setElideMode.

2


The response of Luiz helped me, however I needed something to do automatically, I thought to use the paintEvent or something like that, but the resizeEvent worked well,

  • 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 so:

#include "elidedlabel.h";

...

ElidedLabel label1;

Or:

ElidedLabel *label1 = new ElidedLabel;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.