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
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
Just call this function and | Just call this function and you're good to go. | ||
<code><br />//! Set the elide mode used for displaying text.<br /> void setElideMode(Qt::TextElideMode elideMode) {<br /> elideMode_ = elideMode;<br /> updateGeometry();<br /> }<br /></code> | |||
<code>#ifndef ELIDEDLABEL_H<br />#define ELIDEDLABEL_H | |||
#include <QLabel&gt; | |||
/*<br />MIT - Yash : Speedovation.com [ Picked from internet and modified. if owner needs to add or change license do let me know.]<br />*/ | |||
class ElidedLabel : public QLabel {<br /> Q_OBJECT | |||
public:<br /> ElidedLabel(QWidget '''parent = 0, Qt::WindowFlags f = 0);<br /> ElidedLabel(const QString &txt, QWidget''' parent = 0, Qt::WindowFlags f = 0);<br /> ElidedLabel(const QString &txt,<br /> Qt::TextElideMode elideMode = Qt::ElideRight,<br /> QWidget * parent = 0,<br /> Qt::WindowFlags f = 0); | |||
public:<br /> //! Set the elide mode used for displaying text.<br /> void setElideMode(Qt::TextElideMode elideMode) {<br /> elideMode_ = elideMode;<br /> updateGeometry();<br /> } | |||
//! Get the elide mode currently used to display text.<br /> Qt::TextElideMode elideMode() const { return elideMode_; } | |||
public: // QLabel overrides<br /> void setText(const QString &); | |||
protected: // QLabel overrides<br /> void paintEvent(QPaintEvent ''');<br /> void resizeEvent(QResizeEvent'''); | |||
protected:<br /> //! Cache the elided text so as to not recompute it every paint event<br /> void cacheElidedText(int w); | |||
private:<br /> Qt::TextElideMode elideMode_;<br /> QString cachedElidedText;<br />}; | |||
#endif // ELIDEDLABEL_HPP<br /></code> | |||
<code>#include "ElidedLabel.h&quot; | |||
#include <QDebug&gt;<br />#include <QPainter&gt;<br />#include <QResizeEvent&gt; | |||
ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)<br /> : QLabel(parent, f) , elideMode_(Qt::ElideRight)<br />{<br /> // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);<br />} | |||
ElidedLabel::ElidedLabel(const QString &txt, QWidget *parent, Qt::WindowFlags f)<br /> : QLabel(txt, parent, f) , elideMode_(Qt::ElideRight)<br />{<br />} | |||
ElidedLabel::ElidedLabel(const QString &txt, Qt::TextElideMode elideMode,<br /> QWidget *parent, Qt::WindowFlags f) : QLabel(txt, parent, f)<br /> , elideMode_(elideMode)<br />{<br />} | |||
void ElidedLabel::setText(const QString &txt) {<br /> QLabel::setText(txt);<br /> cacheElidedText(geometry().width());<br />} | |||
void ElidedLabel::cacheElidedText(int w) {<br /> cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);<br />} | |||
void ElidedLabel::resizeEvent(QResizeEvent *e) {<br /> QLabel::resizeEvent(e);<br /> cacheElidedText(e->size().width());<br />} | |||
void ElidedLabel::paintEvent(QPaintEvent *e) {<br /> if(elideMode_ == Qt::ElideNone) {<br /> QLabel::paintEvent(e);<br /> } else {<br /> QPainter p(this);<br /> p.drawText(0, 0,<br /> geometry().width(),<br /> geometry().height(),<br /> alignment(),<br /> cachedElidedText);<br /> }<br />} | |||
</code> |
Revision as of 06:36, 24 February 2015
Just call this function and you're good to go.
<br />//! Set the elide mode used for displaying text.<br /> void setElideMode(Qt::TextElideMode elideMode) {<br /> elideMode_ = elideMode;<br /> updateGeometry();<br /> }<br />
#ifndef ELIDEDLABEL_H<br />#define ELIDEDLABEL_H
#include <QLabel&gt;
/*<br />MIT - Yash : Speedovation.com [ Picked from internet and modified. if owner needs to add or change license do let me know.]<br />*/
class ElidedLabel : public QLabel {<br /> Q_OBJECT
public:<br /> ElidedLabel(QWidget '''parent = 0, Qt::WindowFlags f = 0);<br /> ElidedLabel(const QString &txt, QWidget''' parent = 0, Qt::WindowFlags f = 0);<br /> ElidedLabel(const QString &txt,<br /> Qt::TextElideMode elideMode = Qt::ElideRight,<br /> QWidget * parent = 0,<br /> Qt::WindowFlags f = 0);
public:<br /> //! Set the elide mode used for displaying text.<br /> void setElideMode(Qt::TextElideMode elideMode) {<br /> elideMode_ = elideMode;<br /> updateGeometry();<br /> }
//! Get the elide mode currently used to display text.<br /> Qt::TextElideMode elideMode() const { return elideMode_; }
public: // QLabel overrides<br /> void setText(const QString &);
protected: // QLabel overrides<br /> void paintEvent(QPaintEvent ''');<br /> void resizeEvent(QResizeEvent''');
protected:<br /> //! Cache the elided text so as to not recompute it every paint event<br /> void cacheElidedText(int w);
private:<br /> Qt::TextElideMode elideMode_;<br /> QString cachedElidedText;<br />};
#endif // ELIDEDLABEL_HPP<br />
#include "ElidedLabel.h&quot;
#include <QDebug&gt;<br />#include <QPainter&gt;<br />#include <QResizeEvent&gt;
ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)<br /> : QLabel(parent, f) , elideMode_(Qt::ElideRight)<br />{<br /> // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);<br />}
ElidedLabel::ElidedLabel(const QString &txt, QWidget *parent, Qt::WindowFlags f)<br /> : QLabel(txt, parent, f) , elideMode_(Qt::ElideRight)<br />{<br />}
ElidedLabel::ElidedLabel(const QString &txt, Qt::TextElideMode elideMode,<br /> QWidget *parent, Qt::WindowFlags f) : QLabel(txt, parent, f)<br /> , elideMode_(elideMode)<br />{<br />}
void ElidedLabel::setText(const QString &txt) {<br /> QLabel::setText(txt);<br /> cacheElidedText(geometry().width());<br />}
void ElidedLabel::cacheElidedText(int w) {<br /> cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);<br />}
void ElidedLabel::resizeEvent(QResizeEvent *e) {<br /> QLabel::resizeEvent(e);<br /> cacheElidedText(e->size().width());<br />}
void ElidedLabel::paintEvent(QPaintEvent *e) {<br /> if(elideMode_ == Qt::ElideNone) {<br /> QLabel::paintEvent(e);<br /> } else {<br /> QPainter p(this);<br /> p.drawText(0, 0,<br /> geometry().width(),<br /> geometry().height(),<br /> alignment(),<br /> cachedElidedText);<br /> }<br />}