17

Is QgsMapLayerRegistry going to be availabe in pyqgis with QGIS3

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?

24

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.

  • i think the answer is still incomplete. – Abhijit Gujar Jun 26 '18 at 11:34
  • 1
    I think you need to give more information why you think the answer is still incomplete. – Matthias Kuhn Jun 26 '18 at 11:46
  • if you use QgsProject.instance() how does that solve the singleton issue? – Mickey Perlstein Oct 10 '18 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. – Matthias Kuhn Oct 14 '18 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 – Mickey Perlstein Oct 21 '18 at 10:32
10

You can do the same thing easily just do

 QgsProject.instance().addMapLayer(your_Qgs_whaterver_Layer)

fro more details

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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