From 8ad3d835edca22510b5734cebe5d55db8cfa012d Mon Sep 17 00:00:00 2001 From: Fan PengCheng Date: Wed, 12 May 2021 17:32:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=97=8F=E8=AF=AD?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=8B=E5=AD=97=E4=BD=93=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E4=B8=8D=E5=85=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 藏语字体环境下,计算得到字体高度会出现,换用QFontMetrics的boundingRect函数获取其高度即可 Log: 修复藏语环境下字体显示不全的问题 Bug: https://pms.uniontech.com/zentao/bug-view-79014.html Bug: https://pms.uniontech.com/zentao/bug-view-78763.html Change-Id: I6388b74b1d2930c98d3b4dfb5db496505234c954 --- frame/window/mainwindow.cpp | 24 -------------- frame/window/mainwindow.h | 1 - widgets/tipswidget.cpp | 65 +++++++++++++++++-------------------- widgets/tipswidget.h | 3 +- 4 files changed, 31 insertions(+), 62 deletions(-) diff --git a/frame/window/mainwindow.cpp b/frame/window/mainwindow.cpp index 0bcd29181..19a35fc8d 100755 --- a/frame/window/mainwindow.cpp +++ b/frame/window/mainwindow.cpp @@ -199,30 +199,6 @@ void MainWindow::relaodPlugins() qApp->setProperty("PLUGINSLOADED", true); } -void MainWindow::showEvent(QShowEvent *e) -{ - QWidget::showEvent(e); - - // connect(qGuiApp, &QGuiApplication::primaryScreenChanged, - // windowHandle(), [this](QScreen * new_screen) { - // QScreen *old_screen = windowHandle()->screen(); - // windowHandle()->setScreen(new_screen); - // // 屏幕变化后可能导致控件缩放比变化,此时应该重设控件位置大小 - // // 比如:窗口大小为 100 x 100, 显示在缩放比为 1.0 的屏幕上,此时窗口的真实大小 = 100x100 - // // 随后窗口被移动到了缩放比为 2.0 的屏幕上,应该将真实大小改为 200x200。另外,只能使用 - // // QPlatformWindow直接设置大小来绕过QWidget和QWindow对新旧geometry的比较。 - // const qreal scale = devicePixelRatioF(); - // const QPoint screenPos = new_screen->geometry().topLeft(); - // const QPoint posInScreen = this->pos() - old_screen->geometry().topLeft(); - // const QPoint pos = screenPos + posInScreen * scale; - // const QSize size = this->size() * scale; - - // windowHandle()->handle()->setGeometry(QRect(pos, size)); - // }, Qt::UniqueConnection); - - // windowHandle()->setScreen(qGuiApp->primaryScreen()); -} - void MainWindow::mousePressEvent(QMouseEvent *e) { e->ignore(); diff --git a/frame/window/mainwindow.h b/frame/window/mainwindow.h index bd54189a8..b9882fb88 100644 --- a/frame/window/mainwindow.h +++ b/frame/window/mainwindow.h @@ -136,7 +136,6 @@ public slots: private: using QWidget::show; - void showEvent(QShowEvent *e) override; void mousePressEvent(QMouseEvent *e) override; void keyPressEvent(QKeyEvent *e) override; void enterEvent(QEvent *e) override; diff --git a/widgets/tipswidget.cpp b/widgets/tipswidget.cpp index 569c2576b..69ae6532d 100644 --- a/widgets/tipswidget.cpp +++ b/widgets/tipswidget.cpp @@ -3,13 +3,12 @@ #include #include #include + namespace Dock{ TipsWidget::TipsWidget(QWidget *parent) : QFrame(parent) - , m_width(0) , m_type(SingleLine) { - } void TipsWidget::setText(const QString &text) @@ -21,7 +20,13 @@ void TipsWidget::setText(const QString &text) // 同时去掉两边的空白信息,例如qBittorrent的提示 m_text = document.toPlainText().simplified(); - setFixedSize(fontMetrics().width(m_text) + 20, fontMetrics().height()); +#if 0 //测试时可以使用下面的语句 + // FIXME:藏语字体绘制会有异常,设置高度时需要使用fontMetrics().boundingRect()去获取整体的边界矩形的高度, + // 使用fontMetrics().height()去获取时,针对藏语这种字体,其高度和实际显示区域并不等同 + m_text = "བོད་སྐད་ཡིག་གཟུགས་ཚད་ལེན་ཚོད་ལྟའི་སྐོར་གྱི་རྗོད་ཚིག"; +#endif + + setFixedSize(fontMetrics().width(m_text) + 20, fontMetrics().boundingRect(m_text).height()); update(); @@ -38,15 +43,14 @@ void TipsWidget::setTextList(const QStringList &textList) m_type = TipsWidget::MultiLine; m_textList = textList; - int maxLength = 0; - int k = fontMetrics().height() * m_textList.size(); - setFixedHeight(k); + int width = 0; + int height = 0; for (QString text : m_textList) { - int fontLength = fontMetrics().width(text) + 20; - maxLength = maxLength > fontLength ? maxLength : fontLength; + width = qMax(width, fontMetrics().width(text) + 20); + height += fontMetrics().boundingRect(text).height(); } - m_width = maxLength; - setFixedWidth(maxLength); + + setFixedSize(width, height); update(); } @@ -57,13 +61,9 @@ void TipsWidget::paintEvent(QPaintEvent *event) QPainter painter(this); painter.setPen(QPen(palette().brightText(), 1)); - QTextOption option; - int fontHeight = fontMetrics().height(); - option.setAlignment(Qt::AlignCenter); - QFont textFont; - textFont.setPixelSize(rect().height() / 2 - 1); - painter.setFont(textFont); + QTextOption option; + option.setAlignment(Qt::AlignCenter); switch (m_type) { case SingleLine: { @@ -75,8 +75,9 @@ void TipsWidget::paintEvent(QPaintEvent *event) if (m_textList.size() != 1) option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter); for (QString text : m_textList) { - painter.drawText(QRect(0, y, m_width, fontHeight), text, option); - y += fontHeight; + int lineHeight = fontMetrics().boundingRect(text).height(); + painter.drawText(QRect(0, y, rect().width(), lineHeight), text, option); + y += lineHeight; } } break; @@ -86,23 +87,17 @@ void TipsWidget::paintEvent(QPaintEvent *event) bool TipsWidget::event(QEvent *event) { if (event->type() == QEvent::FontChange) { - if (m_type == SingleLine) { - if (!m_text.trimmed().isEmpty()) { - setFixedSize(fontMetrics().width(m_text) + 6, fontMetrics().height()); - update(); - } - } else { - if (m_textList.size() > 0) { - int maxLength = 0; - setFixedHeight(fontMetrics().height() * m_textList.size()); - for (QString text : m_textList) { - int fontLength = fontMetrics().width(text) + 6; - maxLength = qMax(maxLength,fontLength); - } - m_width = maxLength; - setFixedWidth(maxLength); - update(); - } + switch (m_type) { + case SingleLine: + { + setText(m_text); + break; + } + case MultiLine: + { + setTextList(m_textList); + break; + } } } return QFrame::event(event); diff --git a/widgets/tipswidget.h b/widgets/tipswidget.h index e2e04c9a6..5bf2e0e3b 100644 --- a/widgets/tipswidget.h +++ b/widgets/tipswidget.h @@ -18,7 +18,7 @@ public: const QStringList &textList() { return m_textList; } void setText(const QString &text); void setTextList(const QStringList &textList); - + protected: void paintEvent(QPaintEvent *event) override; bool event(QEvent *event) override; @@ -26,7 +26,6 @@ protected: private: QString m_text; QStringList m_textList; - int m_width; ShowType m_type; }; }