Bug report #19674
Method theme() of QgsMapCanvas not in sync?
|Category:||Python bindings / sipify|
|Affected QGIS version:||3.4.3||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:||invalid|
|Crashes QGIS or corrupts data:||No||Copied to github as #:||27499|
iface.mapCanvas().theme() refers always to an empty string while user set map themes in the QGIS GUI interactively.
In addition you can also check this:
#1 Updated by Adam Szieberth 11 months ago
Connects to #19769
It is weird that
qgc.QgsProject.instance().mapThemeCollection() can provide the list of map themes and can emit a signal when map theme changed but can not give the current/active/actual map theme. Also I found
applyTheme() overcomplicated as I would expect to give the name as a single argument and see the theme changed.
#10 Updated by Martin Dobias 6 months ago
The main map canvas is "special" - it does not have map theme assigned. Instead when you pick a map theme, styles of map layers are modified according to the theme's settings. The theme attribute in QgsMapCanvas is used for other map canvases where user want to follow a specific map theme which is different from the current one.
So in fact this is not a bug, but I understand there's some confusion due to the way how the implementation works.
To figure out the current map theme (and that's how the map themes menu in qgis does it) is like this:
1. get the current state using QgsMapThemeCollection::createThemeFromCurrentState( root, model ) (root and model come from QgsProject::instance())
2. go through the map themes QgsProject::instance()->mapThemeCollection()->mapThemes() and for each have a look at their content QgsProject::instance()->mapThemeCollection()->mapThemeState( name ) and compare it to the current theme's content