Skip to content

Integrate QML Window's Background with the System's Color Palette

Thursday, 24 April 2025  |  KDAB on Qt

Integrate QML Window's Background with the System's Color Palette

As a person who enjoys responsive designs, I like to resize apps and see how quickly the UI updates after the resize. If you're making an app with Qt Quick, you'll sometimes find that, as the window size increases, the UI will lag behind for one or more frames, revealing a white background on two of the window's edges, that usually looks out of place on desktop applications.

Blog_Resizing_Javier_Thumbnail-For-a-Correct-Background-Color-When-Resizing-1

Thumbnail: A window with a white border where there should be a background color.

Why is the color background revealed when the window size is increased?

This issue stems from the fact that Qt Quick windows will render a background color, white by default, before the position of elements is updated by the scene graph, which lags at least one frame behind the resize because it uses a frame buffer. If the default background is white, that means the QML window itself is not aware of your application's theme and that is intentional; after all, only desktop apps need to be aware of the background color used by native applications.

Qt Quick Controls, however, are theme aware, and that may include your platform's theme. This is why, when you make an app for Windows, if you set a Label against the application's default white background, it will look just fine. But when that app is moved to macOS or a Linux distribution with a dark theme, the label's text will be drawn with a light color to match that theme, and that will make it nearly impossible to read against the default white background.

One solution for that issue is to place these components against a control that extends through a large area, such as a Pane. Qt Quick Controls' theme awareness will ensure the right background color is used. However, controls are only drawn within the window, so when the window is resized, its true background color will seep through -- and that's the issue with which we're concerned. The correct solution is to set the window's background to the system's or the theme's background color, which will make the frame buffer's lag far less noticeable during resizing.

How to Make Qt Quick Window Backgrounds Aware of Your System's Theme

Setting a background color is as simple as assigning a value to the "color" property of the window, like so:

Window {
    color: "lightblue"
}

To have it match the background color of native apps, use QML's SystemPallete type, as follows:

import QtQuick
Window {
    color: systemPalette.window
    SystemPalette {
        id: systemPalette
    }
}

Here you can find the various default styles used by Qt Quick for each supported platform. By making use of SystemPalette, the appropriate background color for the current platform will be chosen.

It is important to note that, using SystemPalette, the background color will be chosen based on the current platform and not on which QML style has actually been applied, which may differ if your app makes use of a fixed style for all platforms.

Blog_Resizing_Javier_1-ezgif.com-optimize

Animation of a window resizing, letting the white background seep through a corner.

Blog_Resizing_Javier_2-ezgif.com-optimize

Animation of a window resizing. The background color is consistent with the theme at all times.

Platform Style vs. Fixed Style

As an alternative to following the platform's theme, you could provide a uniform experience for all platforms by enforcing one style for all. There are minor performance advantages to having a fixed style for all systems, but these only tend to matter on low-end embedded hardware, where every cycle counts. There you could use Compile-Time Style Selection, so the QML compiler knows which style is in use and can generate C++ code for bindings.

If you are creating a desktop application, your theming choice should be based, not on performance, but on the level of system integration and the user experience you wish to provide. A photography application might benefit from having a dark mode at all times so the picture colors can pop. An e-mail client, on the other hand, might do better integrating with the system's theme or allowing users to choose between a light theme and a dark theme. Light backgrounds generally provide better contrast, which makes them a good default choice for emails. However, users may prefer the system theme or a dark theme, if it's easier on their eyes.

Blog_Resizing_Javier_Screenshot_20250110_123920

A window from one OS, with a light theme. Its contents can be read just fine.

Blog_Resizing_Javier_Screenshot_20250110_123546

A window from a different OS, with a dark theme. Some of it contents cannot be read due to a lack of contrast.

Crafting Seamless and Theme-Aware Qt Quick Applications

By thoughtfully managing your Qt Quick application's window background color, you can significantly improve its responsiveness and aesthetic integration with the host platform. Whether you opt to align with the system's theme using SystemPalette or enforce a fixed style for uniformity, your choice should reflect the needs of your application and its users. Balancing system integration, performance, and user experience ensures your app not only looks great across platforms but also feels natural to use, providing a polished and professional result. With these tools and insights, you can create dynamic, theme-aware applications that stand out in usability and design.

The post Integrate QML Window's Background with the System's Color Palette appeared first on KDAB.