From 8addf7abe16b74dae7423635280ccd670eaf7428 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Tue, 16 Mar 2021 13:51:48 +0800 Subject: [PATCH] fix(frontend): NameLabel do not update when font-size changed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 修复字体大小更改时主页标签不刷新的bug Log: 修复字体大小更改时主页标签不刷新的bug Bug: http://172.17.66.192/biz/bug-view-42681.html --- src/control/home-page-item.cpp | 18 +++++++++++------- src/mainwindow.cpp | 9 ++++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/control/home-page-item.cpp b/src/control/home-page-item.cpp index 241598e..5fcee60 100644 --- a/src/control/home-page-item.cpp +++ b/src/control/home-page-item.cpp @@ -26,11 +26,18 @@ #include #include #include +#include HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path) : QWidget(parent) { setupUi(type, path); m_transparency = 0.06; + connect(qApp, &QApplication::paletteChanged, this, [ = ]() { + if (m_namelabel) { + QString name = this->toolTip(); + m_namelabel->setText(m_namelabel->fontMetrics().elidedText(name, Qt::ElideRight, 108)); + } + }); } HomePageItem::~HomePageItem() @@ -74,7 +81,6 @@ void HomePageItem::setupUi(const int& type, const QString& path) { }); m_iconlabel = new QLabel(m_widget); m_namelabel = new QLabel(m_widget); - m_namelabel->setStyleSheet("QLabel{color: palette(text);}"); if (type == ItemType::Recent) { m_widget->setFixedSize(300, 48); QIcon icon; @@ -148,20 +154,20 @@ bool HomePageItem::eventFilter(QObject *watched, QEvent *event){ if (watched == m_widget){ if (event->type() == QEvent::MouseButtonPress) { m_transparency = 0.06; - this->repaint(); + this->update(); return true; } else if (event->type() == QEvent::MouseButtonRelease) { Q_EMIT this->onItemClicked(); m_transparency = 0.06; - this->repaint(); + this->update(); return true; } else if (event->type() == QEvent::Enter) { m_transparency = 0.15; - this->repaint(); + this->update(); return true; } else if (event->type() == QEvent::Leave) { m_transparency = 0.06; - this->repaint(); + this->update(); return true; } } @@ -171,12 +177,10 @@ bool HomePageItem::eventFilter(QObject *watched, QEvent *event){ void HomePageItem::paintEvent(QPaintEvent *event) { Q_UNUSED(event) - QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); - QRect rect = this->rect(); p.setRenderHint(QPainter::Antialiasing); // 反锯齿; p.setBrush(opt.palette.color(QPalette::Text)); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 82ed0b8..ef1ecb3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -90,6 +90,11 @@ MainWindow::MainWindow(QWidget *parent) : connect(qApp, &QApplication::paletteChanged, this, [ = ](const QPalette &pal) { this->setPalette(pal); this->update(); + Q_FOREACH (QWidget *widget, this->findChildren()) { + if (widget) { + widget->update(); + } + } }); m_search_result_file = new QQueue; @@ -163,7 +168,6 @@ void MainWindow::initUi() QVBoxLayout * mainlayout = new QVBoxLayout(m_frame); mainlayout->setContentsMargins(8, 0, 8, 6); m_frame->setLayout(mainlayout); - m_frame->setStyleSheet("QLabel{color: palette(text);}"); m_titleFrame = new QFrame(m_frame);//标题栏 m_titleFrame->setFixedHeight(40); @@ -190,8 +194,7 @@ void MainWindow::initUi() m_settingsWidget->show(); connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() { QTimer::singleShot(100, this, [ = ] { - clearSearchResult(); -// m_search_result_thread->start(); +// clearSearchResult(); //现暂定从设置页返回主页面不清空搜索结果 this->setWindowState(this->windowState() & ~Qt::WindowMinimized); this->raise(); this->showNormal();