2

The QGIS GUI shows stored connections to data sources in the browser panel (among other places):

enter image description here

How can I get the same information using the PyQGIS API without manually parsing the QGIS3.ini/QgsSettings?

Using the QgsProviderRegistry seems like a red herring as there are more providers than "connection types" (no idea what to call them). Also, some providers that are visible in the browser do not have an implementation of the QgsProviderMetadata.connections() method (e.g. the OAPIF provider) so that seems not to be related.

1 Answer 1

3

Those connections and their info are stored in a QgsSettings-Object (which is a fancy wrapper around a standard QT QSettings object, see the docs at https://qgis.org/pyqgis/master/core/QgsSettings.html and https://doc.qt.io/qt-5/qsettings.html ).

As far as I know, there is no other way, as the QgsSettings is the only place where the connections are stored.

edit: You may get some info using

for p in QgsApplication.dataItemProviderRegistry().providers():
  print(p.name())

You can explore the QgsSettings interactively to find the correct setting string like this (lifted and adapted from this answer to a different question: https://gis.stackexchange.com/a/434438/15183 ).

s = QgsSettings()
for k in s.allKeys():
    if(k.lower().find("wms")>0): # replace wms with what string you want to search for in the settings
        print(k)

If you want to list all keys in a group, use

s = QgsSettings()
s.beginGroup("qgis/connections-xyz")
print(s.allKeys())

This is all done to get the correct setting name.

To get the setting value using

s.value("qgis/connections-xyz/OpenStreetMap/url")
# returns 'https://tile.openstreetmap.org/{z}/{x}/{y}.png'

Note that the setting name you pass to value is relative to any group your QgsSettings object might "be inside of" after you called beginGroup.

For example, both snippets return the same string:

s1 = QgsSettings()
v1 = s1.value("qgis/connections-xyz/OpenStreetMap/url")

s2 = QgsSettings()
s2.beginGroup("qgis/connections-xyz")

v2 = s2.value("OpenStreetMap/url")
print(v1==v2) #True

To get a list of XYZ connections without their sub-values use

s = QgsSettings()
s.beginGroup("qgis/connections-xyz")
s.childGroups()
# returns ['Mapzen Global Terrain', 'OpenStreetMap'] for me

It really is not that intimidating if you play around with it for some time.

2
  • Thanks for the elaborate answer but sorry I explicitly said "without manually parsing the QGIS3.ini/QgsSettings". The format of the QGIS3.ini is very different between different QGIS versions so this is not a easy or save approach. See lists.osgeo.org/pipermail/qgis-developer/2023-June/065957.html Jun 29 at 10:24
  • Yeah, I read the "without QgsSettings" part only after I finished the answer. Sorry. Thanks for the pointer that the settings keys change over QGIS versions. Have you been able to explore the QgsApplication.dataItemProviderRegistry().providers() path? Maybe that is promising, because that's what the QGIS browser seems to be based on.
    – til_b
    Jun 29 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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