<div dir="ltr"><div><b>QgsLayerTreeLayer</b><br></div><div><a href="https://qgis.org/pyqgis/3.34/core/QgsLayerTreeLayer.html">https://qgis.org/pyqgis/3.34/core/QgsLayerTreeLayer.html</a><br></div><div>No setters or getters for source, but the constructor does take a source, and the XML export has a "source" attribute. </div><div><br></div><div><div><b>QgsMapLayer</b><br></div><div><a href="https://qgis.org/pyqgis/3.34/core/QgsMapLayer.html">https://qgis.org/pyqgis/3.34/core/QgsMapLayer.html</a><br></div><div>Constructor takes a source, and there are several getters / setters for source. The XML export has a "datasource" element. </div><div><br></div><div><b>Isnt the source redundant? Or can they have different values? </b></div><div>Are there situations where the QgsLayerTreeLayer source and the QgsMapLayer source (for corresponding pairs) have different values?</div><div><br></div><div>It seems to me like the source should reside only in one of the two objects, and it should be serialized only for that object, and not in two places. Am I missing something?</div><div><br></div><div><b>Is the following a bug?</b></div><div>I have observed the following behavior. I have a Qgz file created with QGIS v. 3.34, where the two source values for a layer are identical (as I would expect, though it seems redundant to have two values in the first place). I move the folder with the Qgz file and a Shapefile (using relative link) to a different location.</div><div><br></div><div>Now I run a pyqgis standalone script, that simply opens the Qgz file, does absolutely nothing, and write the data back to the Qgz file. </div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><font face="monospace"><br></font></div><font face="monospace">print(Qgis.version()) # '3.22.4-Białowieża'</font><div><font face="monospace">QgsApplication.setPrefixPath("/usr", True)</font></div></div><div><div><font face="monospace">project = QgsProject.instance()</font></div></div><div><div><font face="monospace">qgs = QgsApplication([], False)</font></div></div><div><div><font face="monospace">qgs.initQgis()</font></div></div><div><div><font face="monospace">project.read('test.qgz')</font></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span style="font-family:monospace">project.write('test.qgz')</span><br></div></blockquote><div><div><br></div><div>Now the two source values are no longer the same. The QgsMapLayer source has been changed to point to the old location of the Shapefile (before moving the project). </div><div><br></div><div>I have not been able to make the same thing happen by opening and saving using the QGIS GUI. </div><div><br></div><div>Relevant excerpts from XML after running script. The two paths are no longer identical:</div><div>* <font face="monospace"><layer-tree-layer source="./shapefiles/skab/skab.shp" (...)></font><br>* <font face="monospace"><datasource>../shapefiles/skab/skab.shp</datasource></font><br></div><div><br></div><div>Sincerely, Thomas</div></div></div>