Qt for HarmonyOS/user development guide/qt for harmonyos fullscreen main window
English 中文
Qt for HarmonyOS Theme Setting API Documentation
Qt provides a set of APIs on the HarmonyOS (OHOS) platform to control and detect the application's theme mode (Light / Dark / Follow System).
1. API Documentation
1.1 Query Current Theme Status
bool QOhosAppContext::darkThemeActive() const
Used to determine whether the Qt application is currently in dark theme mode.
| Return Value | Description |
|---|---|
| true | Dark theme is currently active |
| false | Light theme is currently active |
1.2 Theme Switch Notification
void QOhosAppContext::darkThemeActiveChanged(bool darkThemeActive)
A signal emitted when the theme of the Qt application is switched.
| Parameter | Description |
|---|---|
| darkThemeActive | true indicates switching to dark theme, false indicates switching to light theme |
Applications can connect to this signal to update the UI style when the theme switches.
1.3 Set Application Theme Mode
void QOhosAppContext::setColorThemeMode(ColorThemeMode mode)
Used to set the theme mode of the Qt application.
ColorThemeMode Enumeration Values
| Enumeration Value | Description |
|---|---|
| LightTheme | Light theme (default theme for Qt applications) |
| DarkTheme | Dark theme |
| FollowSystemSetting | Follow the system theme settings; the Qt application switches synchronously when the system theme changes |
2. Usage Example
The following example demonstrates how to set the theme mode to "Follow System" in a Qt application and receive notifications when the theme changes:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
auto *appContext = QtOhosExtras::QOhosAppContext::instance();
appContext->setColorThemeMode(
QtOhosExtras::QOhosAppContext::ColorThemeMode::FollowSystemSetting);
QLabel *label = new QLabel(&window);
QObject::connect(
appContext,
&QtOhosExtras::QOhosAppContext::darkThemeActiveChanged,
label,
[label](bool darkThemeActive) {
// Theme change callback
// Update UI according to darkThemeActive
}
);
window.show();
return app.exec();
}
3. Notes
- If set to , the application theme will automatically align with the system theme.
FollowSystemSetting - The current theme status can be queried at any time via .
darkThemeActive()
- It is recommended to uniformly update the color configuration of palettes, style sheets, or custom-painted widgets in the theme switch callback.