[Qgis-developer] Open a project file programmatically

Matthias Kuhn matthias at opengis.ch
Tue Mar 17 02:19:48 PDT 2015


Hi,

Does

iface.addProject( path ) work?

Matthias

On 03/17/2015 10:16 AM, Olivier Dalang wrote:
> Hi !
>
> I'm trying to load a project file using python in a plugin, but am
> encountering some problems.
>
> I simply want to have exactly the same behavior as if the user was
> choosing "open" from the project menu, and selecting one particular file.
>
> The C++ QgisApp class has an void openProject(const QString &fileName)
> method which I suspect to do what I want, but if I'm not mistaken,
> there's no access to QgisApp through python.
> The python QgisInterface class has no such method. It only has a
> reference to the QAction triggering the open file dialog, but that's
> not helping.
>
> So I'm using these lines :
>
>         QgsProject.instance().clear()
>         QgsProject.instance().setFileName('C:\mypath\myfile.qgs')
>         QgsProject.instance().read()
>
> It seems to work more or less.
>
> But the behavior is not exactly the same : 
>
> - postgres layers for which I have a stored user/password in the
> browser (not in the project file) are simply ignored (there's not even
> an user/password prompt, nor the handle bad layers dialog).
> - there's no save prompt if the current project was modified
> - I seem to have some crashes every now and then
>
> Is there a way to simply open a file using python given it's path ? Am
> I just missing it ?
>
> Thanks for your help !!
>
> Olivier
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20150317/06dcd31d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20150317/06dcd31d/attachment.pgp>


More information about the Qgis-developer mailing list