fix(frontend): NameLabel do not update when font-size changed.

Description: 修复字体大小更改时主页标签不刷新的bug

Log: 修复字体大小更改时主页标签不刷新的bug
Bug: http://172.17.66.192/biz/bug-view-42681.html
This commit is contained in:
zhangjiaping 2021-03-16 13:51:48 +08:00
parent ad04389bd5
commit 8addf7abe1
2 changed files with 17 additions and 10 deletions

View File

@ -26,11 +26,18 @@
#include <QPainter> #include <QPainter>
#include <QDesktopServices> #include <QDesktopServices>
#include <QUrl> #include <QUrl>
#include <QApplication>
HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path) : QWidget(parent) HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path) : QWidget(parent)
{ {
setupUi(type, path); setupUi(type, path);
m_transparency = 0.06; 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() HomePageItem::~HomePageItem()
@ -74,7 +81,6 @@ void HomePageItem::setupUi(const int& type, const QString& path) {
}); });
m_iconlabel = new QLabel(m_widget); m_iconlabel = new QLabel(m_widget);
m_namelabel = new QLabel(m_widget); m_namelabel = new QLabel(m_widget);
m_namelabel->setStyleSheet("QLabel{color: palette(text);}");
if (type == ItemType::Recent) { if (type == ItemType::Recent) {
m_widget->setFixedSize(300, 48); m_widget->setFixedSize(300, 48);
QIcon icon; QIcon icon;
@ -148,20 +154,20 @@ bool HomePageItem::eventFilter(QObject *watched, QEvent *event){
if (watched == m_widget){ if (watched == m_widget){
if (event->type() == QEvent::MouseButtonPress) { if (event->type() == QEvent::MouseButtonPress) {
m_transparency = 0.06; m_transparency = 0.06;
this->repaint(); this->update();
return true; return true;
} else if (event->type() == QEvent::MouseButtonRelease) { } else if (event->type() == QEvent::MouseButtonRelease) {
Q_EMIT this->onItemClicked(); Q_EMIT this->onItemClicked();
m_transparency = 0.06; m_transparency = 0.06;
this->repaint(); this->update();
return true; return true;
} else if (event->type() == QEvent::Enter) { } else if (event->type() == QEvent::Enter) {
m_transparency = 0.15; m_transparency = 0.15;
this->repaint(); this->update();
return true; return true;
} else if (event->type() == QEvent::Leave) { } else if (event->type() == QEvent::Leave) {
m_transparency = 0.06; m_transparency = 0.06;
this->repaint(); this->update();
return true; return true;
} }
} }
@ -171,12 +177,10 @@ bool HomePageItem::eventFilter(QObject *watched, QEvent *event){
void HomePageItem::paintEvent(QPaintEvent *event) { void HomePageItem::paintEvent(QPaintEvent *event) {
Q_UNUSED(event) Q_UNUSED(event)
QStyleOption opt; QStyleOption opt;
opt.init(this); opt.init(this);
QPainter p(this); QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QRect rect = this->rect(); QRect rect = this->rect();
p.setRenderHint(QPainter::Antialiasing); // 反锯齿; p.setRenderHint(QPainter::Antialiasing); // 反锯齿;
p.setBrush(opt.palette.color(QPalette::Text)); p.setBrush(opt.palette.color(QPalette::Text));

View File

@ -90,6 +90,11 @@ MainWindow::MainWindow(QWidget *parent) :
connect(qApp, &QApplication::paletteChanged, this, [ = ](const QPalette &pal) { connect(qApp, &QApplication::paletteChanged, this, [ = ](const QPalette &pal) {
this->setPalette(pal); this->setPalette(pal);
this->update(); this->update();
Q_FOREACH (QWidget *widget, this->findChildren<QWidget *>()) {
if (widget) {
widget->update();
}
}
}); });
m_search_result_file = new QQueue<QString>; m_search_result_file = new QQueue<QString>;
@ -163,7 +168,6 @@ void MainWindow::initUi()
QVBoxLayout * mainlayout = new QVBoxLayout(m_frame); QVBoxLayout * mainlayout = new QVBoxLayout(m_frame);
mainlayout->setContentsMargins(8, 0, 8, 6); mainlayout->setContentsMargins(8, 0, 8, 6);
m_frame->setLayout(mainlayout); m_frame->setLayout(mainlayout);
m_frame->setStyleSheet("QLabel{color: palette(text);}");
m_titleFrame = new QFrame(m_frame);//标题栏 m_titleFrame = new QFrame(m_frame);//标题栏
m_titleFrame->setFixedHeight(40); m_titleFrame->setFixedHeight(40);
@ -190,8 +194,7 @@ void MainWindow::initUi()
m_settingsWidget->show(); m_settingsWidget->show();
connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() { connect(m_settingsWidget, &SettingsWidget::settingWidgetClosed, this, [ = ]() {
QTimer::singleShot(100, this, [ = ] { QTimer::singleShot(100, this, [ = ] {
clearSearchResult(); // clearSearchResult(); //现暂定从设置页返回主页面不清空搜索结果
// m_search_result_thread->start();
this->setWindowState(this->windowState() & ~Qt::WindowMinimized); this->setWindowState(this->windowState() & ~Qt::WindowMinimized);
this->raise(); this->raise();
this->showNormal(); this->showNormal();