Skip to content

Commit

Permalink
fix: the widget's theme is not updated after changing parent
Browse files Browse the repository at this point in the history
Change-Id: I744c4e15b0663c2d8eafdf74ce7d2ff0630bde28
  • Loading branch information
zccrs committed Jan 22, 2018
1 parent ff9a89d commit 3cb8f27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/widgets/dapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ bool DApplication::notify(QObject *obj, QEvent *event)
menu->setStyle(style);
}
}
} else if (event->type() == QEvent::ParentChange) {
if (QWidget *widget = qobject_cast<QWidget*>(obj)) {
DThemeManager::instance()->updateThemeOnParentChanged(widget);
}
}

return QApplication::notify(obj, event);
Expand Down
30 changes: 30 additions & 0 deletions src/widgets/dthememanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,35 @@ DThemeManager::DThemeManager()
setTheme("light");
}

static void updateWidgetTheme(DThemeManager *manager, QWidget *widget, QWidget *baseWidget, const QString &theme)
{
inseritStyle(widget, baseWidget);

Q_EMIT manager->widgetThemeChanged(widget, theme);

for (QObject *child : widget->children()) {
if (QWidget *cw = qobject_cast<QWidget*>(child)) {
if (widget->property("_d_dtk_theme").isValid())
return;

updateWidgetTheme(manager, cw, baseWidget, theme);
}
}
}

void DThemeManager::updateThemeOnParentChanged(QWidget *widget)
{
if (widget->property("_d_dtk_theme").isValid())
return;

QWidget *base_widget = nullptr;
const QString &theme = this->theme(widget, &base_widget);

if (!base_widget)
return;

updateWidgetTheme(this, widget, base_widget, theme);
}


DWIDGET_END_NAMESPACE
5 changes: 5 additions & 0 deletions src/widgets/dthememanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public Q_SLOTS:
protected:
DThemeManager();

void updateThemeOnParentChanged(QWidget *widget);

QString m_theme;

private:
friend class DApplication;
};

DWIDGET_END_NAMESPACE
Expand Down

0 comments on commit 3cb8f27

Please sign in to comment.