6

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:

self.iface.addVectorLayer("C:/abc.shp","Global","ogr")

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

6

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

5

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")
    pass

enter image description here

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.