36

Is QgsMapLayerRegistry going to be availabe in PyQGIS with QGIS 3?

When I typed it into the QGIS python console (2.99), python seems to recognize it because it auto-completes qgis.core.QgsMapLayerRegistry, but then gives an error that it's not there:

Traceback (most recent call last):
  File "/usr/lib/python3.5/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
AttributeError: module 'qgis.core' has no attribute 'QgsMapLayerRegistry'

I tried this with two versions of QGIS 2.99 (one in 01/07 and on in 06/17). Both versions give the same error. import doesn't work either:

>>>from qgis.core import QgsMapLayerRegistry
Traceback (most recent call last):
  File "/usr/lib/python3.5/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
ImportError: cannot import name 'QgsMapLayerRegistry'

Is QgsMapLayerRegistry removed in QGIS3?

0

3 Answers 3

47

Yes it has been removed, but alternatives exist.

Changes of the 3.0 API are listed here:

https://qgis.org/api/api_break.html

QgsMapLayerRegistry: Its functionality has been moved to QgsProject.

The main reason is, that QgsMapLayerRegistry is/was a singleton and therefore only a single registry could ever exist. With this constraint removed, major roadblocks are out of the way to implement new things like having multiple projects open side-by-side.

6
  • i think the answer is still incomplete. Commented Jun 26, 2018 at 11:34
  • 2
    I think you need to give more information why you think the answer is still incomplete. Commented Jun 26, 2018 at 11:46
  • if you use QgsProject.instance() how does that solve the singleton issue? Commented Oct 10, 2018 at 16:41
  • @MickeyPerlstein you are right, if you use QgsProject.instance() it's not solved. However, standalone apps can already now be implemented with multiple projects and the QGIS server code is moved in this direction too. In the long run QgsProject.instance() might completely disappear. Commented Oct 14, 2018 at 7:32
  • i dont understand how moving from using a singleton on one part to a singleton on another part changes the ability to run multiple instances (that anyway recieve the same singleton instance (by definition)... wouldn't it make more sense to have a singleton app that gets running instances ? this would also allow much easier interopability and extensibilty (kind of like microsoft office sdk where you can connect to an existing open instance of office or open a new instance Commented Oct 21, 2018 at 10:32
25

You can do the same thing easily just do:

 QgsProject.instance().addMapLayer(your_Qgs_whaterver_Layer)

for more details, see the following:

https://qgis.org/api/classQgsProject.html#a322fce15a4dd7505398ef9d24295f0c1

1
  • Thanks for this answer. It has saved me while migrating some PyQGIS scripts from QGIS2 to QGIS3 Commented May 26, 2021 at 15:47
0

In standalone way:

"""remove layer from memory"""
QgsProject.instance().addMapLayer(your_layer)
QgsProject.instance().removeMapLayer(your_layer.id())

Your Answer

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

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