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);
explicit 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
protected:
//! Cache the elided text so as to not recompute it every paint event
void cacheElidedText(int w);
private:
Qt::TextElideMode elideMode_;
};
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);
explicit 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:
virtual void paintEvent(QPaintEvent* );
virtual 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_;
};
To copy to clipboard, switch view to plain text mode
ElidedLabel
::ElidedLabel(QWidget* parent, Qt
::WindowFlags f
): elideMode_(Qt::ElideRight)
{
}
ElidedLabel
::ElidedLabel(const QString
& text,
QWidget* parent, Qt
::WindowFlags f
): elideMode_(Qt::ElideRight)
{
}
ElidedLabel
::ElidedLabel(const QString
& text, Qt
::TextElideMode elideMode,
QWidget* parent, Qt
::WindowFlags f
) : elideMode_(elideMode)
{
}
void ElidedLabel::setText(const QString& text)
{
cacheElidedText(geometry().width());
}
void ElidedLabel::cacheElidedText(int w)
{
cachedElidedText_ = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
}
{
if(elideMode_ == Qt::ElideNone) {
} else {
p.drawText(0, 0,
geometry().width(),
geometry().height(),
alignment(),
cachedElidedText_);
}
}
{
cacheElidedText(e->size().width());
}
ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f):
QLabel(parent, f),
elideMode_(Qt::ElideRight)
{
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
}
ElidedLabel::ElidedLabel(const QString& text, QWidget* parent, Qt::WindowFlags f):
QLabel(text, parent, f),
elideMode_(Qt::ElideRight)
{
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
}
ElidedLabel::ElidedLabel(const QString& text, Qt::TextElideMode elideMode, QWidget* parent, Qt::WindowFlags f) :
QLabel(text, parent, f),
elideMode_(elideMode)
{
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
}
void ElidedLabel::setText(const QString& text)
{
QLabel::setText(text);
cacheElidedText(geometry().width());
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
}
void ElidedLabel::cacheElidedText(int w)
{
cachedElidedText_ = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
}
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_);
}
}
void ElidedLabel::resizeEvent(QResizeEvent* e)
{
QLabel::resizeEvent(e);
cacheElidedText(e->size().width());
}
To copy to clipboard, switch view to plain text mode
Bookmarks