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 5: | Line 5: | ||
<code>#ifndef ELIDEDLABEL_H<br />#define ELIDEDLABEL_H | <code>#ifndef ELIDEDLABEL_H<br />#define ELIDEDLABEL_H | ||
#include | #include <QLabel> | ||
/*<br />MIT - Yash : Speedovation.com [ Picked from internet and modified. if owner needs to add or change license do let me know.]<br />*/ | /*<br />MIT - Yash : Speedovation.com [ Picked from internet and modified. if owner needs to add or change license do let me know.]<br />*/ | ||
Line 27: | Line 27: | ||
#endif // ELIDEDLABEL_HPP<br /></code> | #endif // ELIDEDLABEL_HPP<br /></code> | ||
<code>#include | <code>#include "ElidedLabel.h" | ||
#include | #include <QDebug><br />#include <QPainter><br />#include <QResizeEvent> | ||
ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)<br /> : QLabel(parent, f) , elideMode_(Qt::ElideRight)<br />{<br /> // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);<br />} | ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)<br /> : QLabel(parent, f) , elideMode_(Qt::ElideRight)<br />{<br /> // setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);<br />} | ||
Line 41: | Line 41: | ||
void ElidedLabel::cacheElidedText(int w) {<br /> cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);<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- | 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 />} | 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> | </code> |
Revision as of 06:52, 25 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>
/*<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"
#include <QDebug><br />#include <QPainter><br />#include <QResizeEvent>
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 />}