15

With PyQGIS, you can change various settings such as making the groups in the Layers Panel (or table of contents) bold:

from PyQt4.QtCore import QSettings
QSettings().setValue("/Qgis/legendGroupsBold", True)

The settings are written which can be seen from the options or from typing:

QSettings().value("/Qgis/legendGroupsBold")
>>> u'true'

But it does not get applied. I thought using

QSettings().sync()

might help but unfortunately not. The only way for it to apply would be to either go to the GUI (Settings > Options) then click OK or restart QGIS.

Is there a method to doing this through PyQGIS?


Context:

I want to add this as a macro in a project file to ensure (for aesthetic purposes) that only group names are bold and layer names are not. Therefore, it would be ideal if the QSettings are applied when the project has loaded (the initial settings would be reapplied when the project closed).

  • When you call sync() to commit your changes, do you then call status()? – whyzar Oct 25 '17 at 19:25
  • @whyzar - Thanks but tested that too and still no change. – Joseph Oct 26 '17 at 11:52
  • 2
    It's so strange,maybe a bug because occurs the same using QSettings().setValue("/qgis/legendLayersBold", False) .I think that the issue is in this line github.com/qgis/QGIS/blob/release-2_18/src/app/… – Fran Raga Oct 26 '17 at 18:19
  • 1
    the possible method is QgisApp::instance()->legend()->updateLegendItemStyles(); but in a future version probably? – Fran Raga Oct 26 '17 at 18:23
  • @FranciscoRaga - Thanks for the link, it does seem like a bug. I was thinking of posting a bug report but with QGIS 3.0 coming out soon, I'll wait and see if this problem still exists in the new version =) – Joseph Oct 27 '17 at 9:39
2
+100

I think it's not possible unless you write a key with the default state into your \HKEY_CURRENT_USER\Software\QGIS\qgis\UI\state take a look here.

actually, when we restart the software it automatically does this for us.

check this solution:

#!/usr/bin/python
from PyQt4.QtCore import QSettings
QSettings( "QGIS", "QGIS2" ).remove( "/UI/state" )
QSettings( "QGIS", "QGIS2" ).remove( "/ComposerUI/state" )


#(for 1.8 it's QGIS instead of QGIS2 IIRC).

if it does not work, you have to write a script to rewrite that registry value.

to do so you need to use winreg to manipulate registry values.

you can view your registry keys via regedit.exe if you use windows.

I hope this helps you...

  • Thanks for your answer, I think I would still have to restart QGIS when the key is written by the macro for QGIS to read it during startup. Still trying to find a way :) – Joseph Oct 27 '17 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.