Jump to content

Qt for HarmonyOS/qtohosextras doc/Color Theme Example

From Qt Wiki
Revision as of 05:24, 17 September 2026 by Shawn Luo (talk | contribs) (Drop manual breadcrumb, MediaWiki renders it automatically (QTFOROH-2307))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Color Theme Example

Demonstrates selecting the application color theme and reacting to dark and light theme changes.


The window offers three radio buttons that map to QOhosAppContext::ColorThemeMode::LightTheme, ColorThemeMode::DarkTheme and ColorThemeMode::FollowSystemSetting. Each selection is applied with QOhosAppContext::setColorThemeMode(). A label shows the effective theme; it is initialized from QOhosAppContext::darkThemeActive() and updated through the QOhosAppContext::darkThemeActiveChanged() signal.

A theme mode is applied when the user clicks one of the radio buttons:

     QObject::connect(
         themeGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), appContext,
         [appContext, themeGroup](auto *) {
             appContext->setColorThemeMode(themeOptions[themeGroup->checkedId()].mode);
         });

The label is kept up to date by connecting to the change signal:

     QObject::connect(
         appContext, &QtOhosExtras::QOhosAppContext::darkThemeActiveChanged, label,
         [label](bool darkThemeActive) {
             label->setText(buildCurrentThemeLabelText(darkThemeActive));
         });

Files: