How does PyQt5 handle system dark and light mode transitions?
-
I am working on a PyQt5 desktop application on macOS Sequoia 15.0 with PyQt5 5.15.10.
While the application is running, when I toggle between the macOS system light and dark mode the application changes the colors of my app, except for the styles I have applied directly with a stylesheet. (See app images below)
- What event in PyQt/Qt is causing this interaction to happen automatically?
- If possible, how do I control or at the very least disable this behavior?
Ideally, I could intercept the event (if it is one) and apply my custom stylings like adding light and dark modes. See the images below as an example. The app was built with dark mode in mind. The issue is when the system changes from dark to light mode the text changes from white to black.
Light Mode
- Widget text is hard to read
Dark Mode
- Styles look as expected
-
Hi and welcome to devnet,
If you where using PyQt/Side6, this stack overflow thread would have given you the information you need.
Note that using style sheets, the style that renders it is not theme nor plateform aware.
-
Hi,
Thanks for the response! I see, so that functionality is available in PyQt6 with the use of
styleHints
andcolorScheme
.I can achieve the functionality I want in PyQt5, but it would require me to manually check the system theme changes automatically in the background.
The downside is that PyQt5 will still send a
palleteChanged
signal/event when the system theme changes. Ideally, I would've been able to block it and implement my own.Either way thanks for the response! Inevitably, I'll likely update PyQt5 to 6 and handle system theme transitions as the stack overflow thread suggests.
-
@Toastito said in How does PyQt5 handle system dark and light mode transitions?:
The downside is that PyQt5 will still send a
palleteChanged
signal/event when the system theme changes. Ideally, I would've been able to block it and implement my own.I haven't tried, but would expect an event filter to be able to filter out PaletteChange events.
The QGuiApplication::paletteChanged signal doesn't appear to be connected to anything by default. QObject::disconnect() without a receiver would ensure that, for any existing objects at the time of the call. Note that disconnecting signals at random can have surprising results.