Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Elided Label: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Categorize)
(Updated to match current coding style, removed unneeded ctors, ...)
Line 1: Line 1:
[[Category:Snippets::Misc]]
[[Category:Snippets::Misc]]
{{LangSwitch}}
{{LangSwitch}}
Just call this function and you're good to go.
Make sure to set the horizontal size policy to minimum and just call this function and you're good to go:


<code>
<code>
// Set the elide mode used for displaying text.
// Set the elide mode used for displaying text.
void setElideMode(Qt::TextElideMode elideMode) {
void ElidedLabel::setElideMode(Qt::TextElideMode elideMode)
     elideMode_ = elideMode;
{
     updateGeometry();
     m_elideMode = elideMode;
     m_cachedText.clear();
    update();
}
}
</code>
</code>
Line 23: Line 25:
     Q_OBJECT
     Q_OBJECT
public:
public:
     ElidedLabel(QWidget* parent=0, Qt::WindowFlags f=0);
     using QLabel::QLabel;
    ElidedLabel(const QString& txt, QWidget* parent=0, Qt::WindowFlags f=0);
    ElidedLabel(const QString& txt,
        Qt::TextElideMode elideMode=Qt::ElideRight,
        QWidget* parent=0,
        Qt::WindowFlags f=0
    );
     // Set the elide mode used for displaying text.
     // Set the elide mode used for displaying text.
     void setElideMode(Qt::TextElideMode elideMode) {
     void setElideMode(Qt::TextElideMode elideMode);
        elideMode_ = elideMode;
        updateGeometry();
    }
     // Get the elide mode currently used to display text.
     // Get the elide mode currently used to display text.
     Qt::TextElideMode elideMode() const { return elideMode_; }
     Qt::TextElideMode elideMode() const { return m_elideMode; }
     // QLabel overrides
 
     void setText(const QString &);
protected:
     void paintEvent(QPaintEvent *e) override;
     void resizeEvent(QResizeEvent *e) override;


protected: // QLabel overrides
private:
     void paintEvent(QPaintEvent*);
     void updateCachedTexts();
    void resizeEvent(QResizeEvent*);
    // Cache the elided text so as to not recompute it every paint event
    void cacheElidedText(int w);


private:
private:
     Qt::TextElideMode elideMode_;
     Qt::TextElideMode m_elideMode = Qt::ElideRight;
     QString cachedElidedText;
     QString m_cachedElidedText;
    QString m_cachedText;
};
};


Line 57: Line 50:
#include "ElidedLabel.h"
#include "ElidedLabel.h"


#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QResizeEvent>
#include <QResizeEvent>


ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f)
void ElidedLabel::setElideMode(Qt::TextElideMode elideMode)
    : QLabel(parent, f)
    , elideMode_(Qt::ElideRight)
{
{
     // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
     m_elideMode = elideMode;
    m_cachedText.clear();
    update();
}
}


ElidedLabel::ElidedLabel(const QString& txt, QWidget* parent, Qt::WindowFlags f)
void ElidedLabel::resizeEvent(QResizeEvent *e)
    : QLabel(txt, parent, f)
    , elideMode_(Qt::ElideRight)
{
{
    QLabel::resizeEvent(e);
    m_cachedText.clear();
}
}


ElidedLabel::ElidedLabel(const QString& txt, Qt::TextElideMode elideMode,
void ElidedLabel::paintEvent(QPaintEvent *e)
    QWidget *parent, Qt::WindowFlags f)
    : QLabel(txt, parent, f)
    , elideMode_(elideMode)
{
{
}
    if (m_elideMode == Qt::ElideNone)
        return QLabel::paintEvent(e);


void ElidedLabel::setText(const QString& txt) {
    updateCachedTexts();
     QLabel::setText(txt);
    QLabel::setText(m_cachedElidedText);
     cacheElidedText(geometry().width());
     QLabel::paintEvent(e);
     QLabel::setText(m_cachedText);
}
}


void ElidedLabel::cacheElidedText(int w) {
void ElidedLabel::updateCachedTexts()
     cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
{
}
     // setText() is not virtual ... :/
 
    const auto txt = text();
void ElidedLabel::resizeEvent(QResizeEvent* e) {
    if (m_cachedText == txt)
    QLabel::resizeEvent(e);
        return;
    cacheElidedText(e->size().width());
    m_cachedText = txt;
    const QFontMetrics fm(fontMetrics());
    m_cachedElidedText = fm.elidedText(text(),
                                      m_elideMode,
                                      width(),
                                      Qt::TextShowMnemonic);
    // make sure to show at least the first character
    if (!m_cachedText.isEmpty())
    {
      const QString showFirstCharacter = m_cachedText.at(0) + QStringLiteral("...");
      setMinimumWidth(fm.horizontalAdvance(showFirstCharacter) + 1);
    }
}
}


void ElidedLabel::paintEvent(QPaintEvent* e) {
    if (elideMode_ == Qt::ElideNone) {
        QLabel::paintEvent(e);
    } else {
        QPainter p(this);
        p.drawText(0, 0,
            geometry().width(),
            geometry().height(),
            alignment(),
            cachedElidedText
        );
    }
}
</code>
</code>

Revision as of 12:46, 17 December 2018

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Make sure to set the horizontal size policy to minimum and just call this function and you're good to go:

// Set the elide mode used for displaying text.
void ElidedLabel::setElideMode(Qt::TextElideMode elideMode)
{
    m_elideMode = elideMode;
    m_cachedText.clear();
    update();
}
#ifndef ELIDEDLABEL_H
#define ELIDEDLABEL_H

#include <QLabel>

// MIT - Yash : Speedovation.com [ Picked from internet and modified. if owner needs to add or change license do let me know.]

class ElidedLabel : public QLabel 
{
    Q_OBJECT
public:
    using QLabel::QLabel;
    // Set the elide mode used for displaying text.
    void setElideMode(Qt::TextElideMode elideMode);
    // Get the elide mode currently used to display text.
    Qt::TextElideMode elideMode() const { return m_elideMode; }

protected:
    void paintEvent(QPaintEvent *e) override;
    void resizeEvent(QResizeEvent *e) override;

private:
    void updateCachedTexts();

private:
    Qt::TextElideMode m_elideMode = Qt::ElideRight;
    QString m_cachedElidedText;
    QString m_cachedText;
};

#endif // ELIDEDLABEL_HPP
#include "ElidedLabel.h"

#include <QPaintEvent>
#include <QResizeEvent>

void ElidedLabel::setElideMode(Qt::TextElideMode elideMode)
{
    m_elideMode = elideMode;
    m_cachedText.clear();
    update();
}

void ElidedLabel::resizeEvent(QResizeEvent *e)
{
    QLabel::resizeEvent(e);
    m_cachedText.clear();
}

void ElidedLabel::paintEvent(QPaintEvent *e)
{
    if (m_elideMode == Qt::ElideNone)
        return QLabel::paintEvent(e);

    updateCachedTexts();
    QLabel::setText(m_cachedElidedText);
    QLabel::paintEvent(e);
    QLabel::setText(m_cachedText);
}

void ElidedLabel::updateCachedTexts()
{
    // setText() is not virtual ... :/
    const auto txt = text();
    if (m_cachedText == txt)
        return;
    m_cachedText = txt;
    const QFontMetrics fm(fontMetrics());
    m_cachedElidedText = fm.elidedText(text(),
                                       m_elideMode,
                                       width(),
                                       Qt::TextShowMnemonic);
    // make sure to show at least the first character
    if (!m_cachedText.isEmpty())
    {
      const QString showFirstCharacter = m_cachedText.at(0) + QStringLiteral("...");
      setMinimumWidth(fm.horizontalAdvance(showFirstCharacter) + 1);
    }
}