[GIS] How to apply changes in QSettings immediately

macropyqgistable of contents

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).

Best Answer

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...