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
(Decode HTML entity names)
(Cleanup)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
Just call this function and you're good to go.
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 setElideMode(Qt::TextElideMode elideMode) {
elideMode_ = elideMode;
    elideMode_ = elideMode;
updateGeometry();
    updateGeometry();
}
}
</code>
</code>


<code>#ifndef ELIDEDLABEL_H
<code>
#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
 
public:
ElidedLabel(QWidget '''parent = 0, Qt::WindowFlags f = 0);
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);


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


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


private:
private:
Qt::TextElideMode elideMode_;
    Qt::TextElideMode elideMode_;
QString cachedElidedText;
    QString cachedElidedText;
};
};


Line 60: Line 53:
</code>
</code>


<code>#include "ElidedLabel.h"
<code>
#include "ElidedLabel.h"


#include <QDebug>
#include <QDebug>
Line 66: Line 60:
#include <QResizeEvent>
#include <QResizeEvent>


ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)
ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f)
: QLabel(parent, f) , elideMode_(Qt::ElideRight)
    : QLabel(parent, f)  
    , elideMode_(Qt::ElideRight)
{
{
// setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
}
}


ElidedLabel::ElidedLabel(const QString &txt, QWidget *parent, Qt::WindowFlags f)
ElidedLabel::ElidedLabel(const QString& txt, QWidget* parent, Qt::WindowFlags f)
: QLabel(txt, parent, f) , elideMode_(Qt::ElideRight)
    : QLabel(txt, parent, f)  
    , elideMode_(Qt::ElideRight)
{
{
}
}


ElidedLabel::ElidedLabel(const QString &txt, Qt::TextElideMode elideMode,
ElidedLabel::ElidedLabel(const QString& txt, Qt::TextElideMode elideMode,
QWidget *parent, Qt::WindowFlags f) : QLabel(txt, parent, f)
    QWidget *parent, Qt::WindowFlags f)  
, elideMode_(elideMode)
    : QLabel(txt, parent, f)
    , elideMode_(elideMode)
{
{
}
}


void ElidedLabel::setText(const QString &txt) {
void ElidedLabel::setText(const QString& txt) {
QLabel::setText(txt);
    QLabel::setText(txt);
cacheElidedText(geometry().width());
    cacheElidedText(geometry().width());
}
}


void ElidedLabel::cacheElidedText(int w) {
void ElidedLabel::cacheElidedText(int w) {
cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
    cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
}
}


void ElidedLabel::resizeEvent(QResizeEvent *e) {
void ElidedLabel::resizeEvent(QResizeEvent* e) {
QLabel::resizeEvent(e);
    QLabel::resizeEvent(e);
cacheElidedText(e->size().width());
    cacheElidedText(e->size().width());
}
}


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

Revision as of 17:33, 28 June 2015

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

Just call this function and you're good to go.

// Set the elide mode used for displaying text.
void setElideMode(Qt::TextElideMode elideMode) {
    elideMode_ = elideMode;
    updateGeometry();
}
#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:
    ElidedLabel(QWidget* parent=0, Qt::WindowFlags f=0);
    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.
    void setElideMode(Qt::TextElideMode elideMode) {
        elideMode_ = elideMode;
        updateGeometry();
    }
    // Get the elide mode currently used to display text.
    Qt::TextElideMode elideMode() const { return elideMode_; }
    // QLabel overrides
    void setText(const QString &);

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

private:
    Qt::TextElideMode elideMode_;
    QString cachedElidedText;
};

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

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

ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f)
    : QLabel(parent, f) 
    , elideMode_(Qt::ElideRight)
{
    // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
}

ElidedLabel::ElidedLabel(const QString& txt, QWidget* parent, Qt::WindowFlags f)
    : QLabel(txt, parent, f) 
    , elideMode_(Qt::ElideRight)
{
}

ElidedLabel::ElidedLabel(const QString& txt, Qt::TextElideMode elideMode,
    QWidget *parent, Qt::WindowFlags f) 
    : QLabel(txt, parent, f)
    , elideMode_(elideMode)
{
}

void ElidedLabel::setText(const QString& txt) {
    QLabel::setText(txt);
    cacheElidedText(geometry().width());
}

void ElidedLabel::cacheElidedText(int w) {
    cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
}

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