I have added a shapefile in QGIS using Python plugin. I want to add this shapefile when QGIS is loading so I have to add code in the initgui() method.

I have used following code in the initgui() method:


But when I open QGIS it asks me to save the file but not add/show the shapefile whether I saved it or not.


It looks like that QGIS creates a new empty project after the initialization. But if you want to open Qgis with the same layer/project you can first save your layer into a project and then define that the project opens on launch. You can find it here:

Options-> General-> Project files-> Open projects on launch

| improve this answer | |

An alternative option would be to call the addVectorLayer method within python macro openProject function (Project>Project Properties | Macros):

def openProject():
    from qgis.utils import iface
    iface.addVectorLayer("c:/Temp/AA/counties.shp", "ccounty", "ogr")

enter image description here

| improve this answer | |

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.