1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

only show tooltip if required

This commit is contained in:
krepa098 2014-08-14 11:42:03 +02:00
parent 1f92ad18a8
commit eefa8d8ae6
2 changed files with 18 additions and 11 deletions

View File

@ -16,24 +16,21 @@
#include "croppinglabel.h" #include "croppinglabel.h"
CroppingLabel::CroppingLabel(QWidget *parent) CroppingLabel::CroppingLabel(QWidget* parent)
: QLabel(parent) : QLabel(parent)
{ {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
} }
void CroppingLabel::setText(const QString &text) void CroppingLabel::setText(const QString& text)
{ {
origText = text; origText = text;
setToolTip(text); setElidedText();
QLabel::setText(fontMetrics().elidedText(text, Qt::ElideRight, width()));
} }
void CroppingLabel::resizeEvent(QResizeEvent *ev) void CroppingLabel::resizeEvent(QResizeEvent* ev)
{ {
setText(origText); setElidedText();
QLabel::resizeEvent(ev); QLabel::resizeEvent(ev);
} }
@ -46,3 +43,14 @@ QSize CroppingLabel::minimumSizeHint() const
{ {
return QSize(fontMetrics().width("..."), QLabel::minimumSizeHint().height()); return QSize(fontMetrics().width("..."), QLabel::minimumSizeHint().height());
} }
void CroppingLabel::setElidedText()
{
QString elidedText = fontMetrics().elidedText(origText, Qt::ElideRight, width());
if (elidedText != origText)
setToolTip(origText);
else
setToolTip(QString());
QLabel::setText(elidedText);
}

View File

@ -30,9 +30,8 @@ public:
virtual QSize sizeHint() const; virtual QSize sizeHint() const;
virtual QSize minimumSizeHint() const; virtual QSize minimumSizeHint() const;
signals: protected:
void setElidedText();
public slots:
private: private:
QString origText; QString origText;