From 1097d65959e0ac65c2548cdba90faa0314c2b26a Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Fri, 9 Sep 2022 09:34:45 +0800 Subject: [PATCH] fix elision for vertical eliding label. --- src/ElidingLabel.cpp | 34 ++++++++++++++++++++++++++++++---- src/ElidingLabel.h | 5 +++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/ElidingLabel.cpp b/src/ElidingLabel.cpp index ee75313..403e24d 100644 --- a/src/ElidingLabel.cpp +++ b/src/ElidingLabel.cpp @@ -118,7 +118,7 @@ Qt::TextElideMode CElidingLabel::elideMode() const void CElidingLabel::setElideMode(Qt::TextElideMode mode) { d->ElideMode = mode; - d->elideText(size().width()); + d->elideText(availableWidthForText()); } //============================================================================ @@ -155,12 +155,26 @@ void CElidingLabel::resizeEvent(QResizeEvent *event) { if (!d->isModeElideNone()) { - d->elideText(event->size().width()); + d->elideText(availableWidthForText(event)); } Super::resizeEvent(event); } +//============================================================================ +int CElidingLabel::availableWidthForText() const +{ + return size().width(); +} + + +//============================================================================ +int CElidingLabel::availableWidthForText(QResizeEvent* event) const +{ + return event->size().width(); +} + + //============================================================================ QSize CElidingLabel::minimumSizeHint() const { @@ -216,7 +230,7 @@ void CElidingLabel::setText(const QString &text) else { internal::setToolTip(this, text); - d->elideText(this->size().width()); + d->elideText(availableWidthForText()); } } @@ -238,7 +252,7 @@ void CVerticalElidingLabel::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.rotate(90); - painter.drawText(0,0, text()); + painter.drawText(0,0, QLabel::text()); } //============================================================================ @@ -254,6 +268,18 @@ QSize CVerticalElidingLabel::minimumSizeHint() const QSize s = CElidingLabel::sizeHint(); return QSize(s.height(), s.width()); } + +//============================================================================ +int CVerticalElidingLabel::availableWidthForText() const +{ + return size().height(); +} + +//============================================================================ +int CVerticalElidingLabel::availableWidthForText(QResizeEvent* event) const +{ + return event->size().height(); +} } // namespace QtLabb //--------------------------------------------------------------------------- diff --git a/src/ElidingLabel.h b/src/ElidingLabel.h index 8dbf051..dcb1163 100644 --- a/src/ElidingLabel.h +++ b/src/ElidingLabel.h @@ -55,6 +55,9 @@ protected: virtual void resizeEvent( QResizeEvent *event ) override; virtual void mouseDoubleClickEvent( QMouseEvent *ev ) override; + virtual int availableWidthForText() const; + virtual int availableWidthForText(QResizeEvent* event) const; + public: using Super = QLabel; @@ -109,6 +112,8 @@ protected: void paintEvent(QPaintEvent* event) override; QSize sizeHint() const override; QSize minimumSizeHint() const override; + int availableWidthForText() const override; + int availableWidthForText(QResizeEvent* event) const override; public: CVerticalElidingLabel(QWidget* parent = 0, Qt::WindowFlags f = Qt::WindowFlags ());