Skip to content

Commit 4d4be08

Browse files
committed
Theme: added ability to access root Theme via attached property
1 parent 9361689 commit 4d4be08

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

ui/StatusQ/include/StatusQ/theme.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class Theme : public QQuickAttachedPropertyPropagator
5858

5959
const ThemePalette* palette() const;
6060

61+
Q_INVOKABLE Theme* rootTheme();
62+
6163
static Theme *qmlAttachedProperties(QObject *object);
6264

6365
signals:

ui/StatusQ/src/theme.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "StatusQ/theme.h"
22

3+
#include <QQmlApplicationEngine>
4+
35
namespace {
46

57
constexpr qreal s_defaultPadding = 16;
@@ -165,6 +167,23 @@ const ThemePalette* Theme::palette() const
165167
: s_paletteDark.get();
166168
}
167169

170+
Theme *Theme::rootTheme()
171+
{
172+
auto theme = qobject_cast<Theme*>(attachedParent());
173+
174+
if (!theme)
175+
return this;
176+
177+
while (true) {
178+
auto next = qobject_cast<Theme*>(theme->attachedParent());
179+
180+
if (!next || qobject_cast<QQmlApplicationEngine*>(next->parent()))
181+
return theme;
182+
else
183+
theme = next;
184+
}
185+
}
186+
168187
Theme* Theme::qmlAttachedProperties(QObject *object)
169188
{
170189
return new Theme(object);

0 commit comments

Comments
 (0)