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
No edit summary
m (Fix code tags -> syntaxhighlight)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Just call this function and you're good to go.
[[Category:Snippets::Misc]]
{{LangSwitch}}
Make sure to set the horizontal size policy to minimum and just call this function and you're good to go:


<code>
<syntaxhighlight lang="c++">
//! 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();
</code>
    update();
}
</syntaxhighlight>


<code>#ifndef ELIDEDLABEL_H
<syntaxhighlight lang="c++">
#ifndef ELIDEDLABEL_H
#define ELIDEDLABEL_H
#define ELIDEDLABEL_H


#include <QLabel>
#include <QLabel>


/*
// MIT - Yash : Speedovation.com [ Picked from internet and modified. if owner needs to add or change license do let me know.]
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


class ElidedLabel : public QLabel
{
    Q_OBJECT
public:
public:
ElidedLabel(QWidget '''parent = 0, Qt::WindowFlags f = 0);
    using QLabel::QLabel;
ElidedLabel(const QString &amp;txt, QWidget''' parent = 0, Qt::WindowFlags f = 0);
    // Set the elide mode used for displaying text.
ElidedLabel(const QString &amp;txt,
    void setElideMode(Qt::TextElideMode elideMode);
Qt::TextElideMode elideMode = Qt::ElideRight,
    // Get the elide mode currently used to display text.
QWidget * parent = 0,
    Qt::TextElideMode elideMode() const { return m_elideMode; }
Qt::WindowFlags f = 0);


public:
protected:
//! Set the elide mode used for displaying text.
    void paintEvent(QPaintEvent *e) override;
void setElideMode(Qt::TextElideMode elideMode) {
    void resizeEvent(QResizeEvent *e) override;
elideMode_ = elideMode;
updateGeometry();
}


//! Get the elide mode currently used to display text.
private:
Qt::TextElideMode elideMode() const { return elideMode_; }
    void updateCachedTexts();
 
public: // QLabel overrides
void setText(const QString &amp;);
 
protected: // QLabel overrides
void paintEvent(QPaintEvent ''');
void resizeEvent(QResizeEvent''');
 
protected:
//! 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;
};
};


#endif // ELIDEDLABEL_HPP
#endif // ELIDEDLABEL_HPP
</code>
</syntaxhighlight>


<code>#include "ElidedLabel.h"
<syntaxhighlight lang="c++">
#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 &amp;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 &amp;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 &amp;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();
    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);
    }
}
}
 
</syntaxhighlight>
void ElidedLabel::resizeEvent(QResizeEvent *e) {
QLabel::resizeEvent(e);
cacheElidedText(e->size().width());
}
 
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>

Latest revision as of 20:33, 17 March 2023

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