Jump to content

Qt for HarmonyOS/qtohosextras doc/Color Theme Example

From Qt Wiki

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: