From 519a2ad6985fafb8b84c32c7a2005fe989d96fcc Mon Sep 17 00:00:00 2001 From: shaojun Date: Wed, 22 Feb 2023 13:42:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9B=BD=E6=A0=87=E8=B6=85=E5=A4=A7?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E9=9B=86=E6=96=87=E5=AD=97=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E4=B8=8D=E5=85=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改字体计算高度方法 Log: Bug: https://pms.uniontech.com/bug-view-187789.html Influence: 通知中心和通知界面在不同字体下的布局。 Change-Id: I463948052b64becad4c623a22a043b6f72642d4c --- dde-osd/notification/appbodylabel.cpp | 14 ++++++++++---- dde-osd/notification/appbodylabel.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dde-osd/notification/appbodylabel.cpp b/dde-osd/notification/appbodylabel.cpp index 071b1ff4..fecbb84f 100644 --- a/dde-osd/notification/appbodylabel.cpp +++ b/dde-osd/notification/appbodylabel.cpp @@ -19,6 +19,10 @@ AppBodyLabel::AppBodyLabel(QWidget *parent) , m_alignment(Qt::AlignVCenter) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + + connect(qApp, &QGuiApplication::fontChanged, this, [&] { + m_fontHeight = fontMetrics().size(Qt::TextSingleLine, text()).height(); + }); } void AppBodyLabel::setText(const QString &text) @@ -32,6 +36,8 @@ void AppBodyLabel::setText(const QString &text) #endif m_text = text; + m_fontHeight = fontMetrics().size(Qt::TextSingleLine, text).height(); + updateLineCount(); update(); } @@ -96,12 +102,12 @@ static int drawText(QPainter *painter, const QRectF &rect, int lineHeight, QText QSize AppBodyLabel::sizeHint() const { - return QSize(width(), fontMetrics().height() * m_lineCount); + return QSize(width(), m_fontHeight * m_lineCount); } QSize AppBodyLabel::minimumSizeHint() const { - QSize size(width(), fontMetrics().height()); + QSize size(width(), m_fontHeight); return size; } @@ -162,7 +168,7 @@ void AppBodyLabel::paintEvent(QPaintEvent *event) layout.setTextOption(option); QRect rect(this->rect()); - int lineHeight = fontMetrics().height(); + int lineHeight = m_fontHeight; int lineCount = m_lineCount; while (lineCount > 1 && lineCount * lineHeight > height()) { @@ -185,5 +191,5 @@ void AppBodyLabel::updateLineCount() layout.setTextOption(option); - m_lineCount = drawText(nullptr, QRectF(QPointF(0, 0), QSizeF(width(), INT_MAX)), fontMetrics().height(), &layout, Qt::ElideNone); + m_lineCount = drawText(nullptr, QRectF(QPointF(0, 0), QSizeF(width(), INT_MAX)), m_fontHeight, &layout, Qt::ElideNone); } diff --git a/dde-osd/notification/appbodylabel.h b/dde-osd/notification/appbodylabel.h index e05ea9b8..991bc136 100644 --- a/dde-osd/notification/appbodylabel.h +++ b/dde-osd/notification/appbodylabel.h @@ -42,6 +42,7 @@ private: QString m_text; int m_lineCount = 0; Qt::Alignment m_alignment; + int m_fontHeight = 0; }; #endif // APPBODYLABEL_H -- 2.20.1