qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.34k stars 2.98k forks source link

locale/userLocale settings key can be NULLed under some circumstances #52359

Open kannes opened 1 year ago

kannes commented 1 year ago

What is the bug or the crash?

References: https://lists.osgeo.org/pipermail/qgis-user/2023-March/052727.html https://github.com/g-sherman/Qgis-Plugin-Builder/issues/116

Steps to reproduce the issue

  1. $ qgis --lang de
  2. "Einstellungen" -> "Optionen" -> "Allgemein" -> "System-Locale übersteuern" -> "Deutsch" (aka de)
  3. Quit QGIS
  4. $ qgis --lang en
  5. "Options" -> "General" -> Uncheck "Override System Locale" (note how its User Interface Translation combobox' current value is empty and grey)

-> QSettings().value('locale/userLocale') now returns NULL

If you try to install/enable affected plugins now, they will crash. You can try "Räumlicher Filter" (up to 1.3.1, until we fix it there) or "'Geometry Paster', 'Geodata to ENVI-met'" according to the linked mailing list thread.

Versions

master

Supported QGIS version

New profile

Additional context

https://github.com/qgis/QGIS/issues/37620 might be related maybe.

For testing use QgsSettings().remove("locale/userLocale").

bru500 commented 1 year ago

This is a nasty error. If in Options - General, 'Override System Local' is selected, very many plugins crash when loaded. We need then to set locale/userLocal manualy via python console: QSettings().setValue('locale/userLocale', 'en_US') But after opening Options window and clicking done, again the option is missing and the plugins crash.

alexbruy commented 10 months ago

This seems not a QGIS issue, but a problem in Plugin Builder and other plugins which are not checking return value before using it. I'd say this can be closed.

kannes commented 6 months ago

Yes absolutely, but many plugins have been built with the Plugin Builder and continue to be build with it. Sadly the Plugin Builder is not getting updated but keeps getting used by plugin developers so this will continue to be an issue. The issue is not obvious to a developer as it might not trigger for them but happens in "other people's environments".

It would be great to implement a workaround in QGIS as a quicker fix.

Another ticket that had collected broken plugins and thoughts: https://github.com/qgis/QGIS/issues/29281

kannes commented 5 months ago

Future reader: Do not use QSettings, but use QgsSettings. If you think you are already using QgsSettings, check again and make sure.

I got a user where QSettings gets NULL as value and thus will not use a default value (if passed e.g. QLocale().name():

>>> type(QSettings().value("locale/userLocale"))
<class 'PyQt5.QtCore.QVariant'>
>>> QSettings().value("locale/userLocale")
NULL
>>> QSettings().value("locale/userLocale", QLocale().name())
NULL

QgsSettings however does return a None and consider that an unset value, making the default replacement work:

>>> type(QgsSettings().value("locale/userLocale"))
<class 'NoneType'>
>>> QgsSettings().value("locale/userLocale")
>>> QgsSettings().value("locale/userLocale", QLocale().name())
'de_DE'

The QGIS3.ini locale section looks like this:

[locale]
userLocale=
globalLocale=
overrideFlag=false
showGroupSeparator=false

Using QSettings works fine if there is no userLocale= line in the ini.


This might actually be a bug of QgsSettings if it disagress with QSettings about what a key without value means?