Bug report #19674

Method theme() of QgsMapCanvas not in sync?

Added by Darius G. over 5 years ago. Updated about 5 years ago.

Status:Closed
Priority:Normal
Assignee:-
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

Description

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:

https://gis.stackexchange.com/questions/293507/how-to-read-out-which-map-theme-is-used-in-qgis-3


Related issues

Related to QGIS Application - Feature request #19769: Add a variable returning the current map theme Open 2018-09-04

History

#1 Updated by Adam Szieberth over 5 years 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.

#2 Updated by Jürgen Fischer over 5 years ago

#3 Updated by Darius G. over 5 years ago

I have installed latest QGIS 3.4 and I have noticed that the bug is still not solved in this version?

#4 Updated by Giovanni Manghi over 5 years ago

  • Affected QGIS version changed from 3.2.2 to 3.4.0

#5 Updated by Darius G. over 5 years ago

Bug still in QGIS 3.4.2 not solved.

#6 Updated by Giovanni Manghi over 5 years ago

  • Affected QGIS version changed from 3.4.0 to 3.4.2
  • Category changed from Python plugins to Python bindings / sipify

#7 Updated by Darius G. over 5 years ago

Bug still in QGIS 3.4.3 not solved.

#8 Updated by Giovanni Manghi over 5 years ago

  • Affected QGIS version changed from 3.4.2 to 3.4.3

#9 Updated by Darius G. about 5 years ago

Bug still in QGIS 3.4.4 not solved...

#10 Updated by Martin Dobias about 5 years 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

#11 Updated by Darius G. about 5 years ago

  • Status changed from Open to Closed

Thank you very much for the feedback. I have implemented your procedure and it works.

#12 Updated by Giovanni Manghi about 5 years ago

  • Resolution set to invalid

Also available in: Atom PDF