I need to read published WFS layers from QGIS Project file, but without success. I try using QgsProject.entryList() method, but only get an empty list as []
:
from qgis.core import *
from PyQt5.QtCore import QFileInfo
qgs = QgsApplication([], False)
print(QgsProject.instance().entryList("WFSLayers", "/"));
qgs.exitQgis()
The WFS layers are published and show up like this in QGS project file:
<WFSLayers type="QStringList">
<value>layer1...</value>
<value>layer2...</value>
</WFSLayers>
Any idea what I'm doing wrong?
Best Answer
As you are in a standalone application according to your code, you need to load your qgs/qgz QGIS project file before trying to get WFS list.
At the moment, you ask to read WFS list from an empty project. So, it will always return an empty result.
Edit: the fact to load QGIS project was only part of the solution. You need to use
instead of
Found looking at QGSProject API doc
Edit 2: Illustrations about the 6 methods
readBoolEntry
,readDoubleEntry
,readEntry
,readListEntry
,subkeyList
andreadNumEntry
to access some of the XML content within your QGIS project file.Difference between
readListEntry
andentryList
due to xml structure in your QGIS project (see below excerpt). Explain whyQgsProject.instance().entryList("WFSLayersPrecision", "/")
works and then you needQgsProject.instance().readListEntry('WFSLayers', '/')
.You can also test the following:
QgsProject.instance().readNumEntry("WFSLayersPrecision", "/ne_110m_admin_0_countries_d24b9f99_da2b_470c_966c_7402b525eace")
,QgsProject.instance().readBoolEntry(f"WMSServiceCapabilities", "/")
QgsProject.instance().readDoubleEntry(f"PAL", "/CandidatesPolygonPerCM")
QgsProject.instance().subkeyList('DBManager', 'savedQueries')
(need to save a SQL query in DB Manager to get results)