How to load a QGIS project in python
Posted by: Gavin Fleming | in QGIS | 9 years, 10 months ago | 4 comments

Today in a project we are working on we wanted to load a QGIS project. It takes  surprisingly few lines of code to make a small standalone application that loads a project and then shows it as a map in a window like this:

 

Here is the code I wrote to produce this:

The main bit of magic is the QgsLayerTreeMapCanvasBridge class which will convert your project into a layer tree so that the layers appear in the canvas. If you ever need to make a standalone python application with a nice map in it, consider using the QGIS API to do it!

Current rating: 3.0

Comments

surendrankn 9 years, 9 months ago

I got an error when it execute the following line
canvas = QgsMapCanvas(None) # will reparent it to widget via layout

"QGraphicsScene::addItem: item has already been added to this scene"

I could not see any solution for this. It displays a blank window

Link | Reply
Currently unrated

different way to build qgis python standalone appl 9 years, 4 months ago

[…] For example here and here. […]

Link | Reply
Currently unrated

How to write a QGIS-plugin to be used in IntraMaps 9 years, 3 months ago

[…] layer is still visible in the map (canvas). I have tried the “bridge” found here and here and different variations of iface.mapCanvas() without any luck so […]

Link | Reply
Currently unrated

ThomasG77 9 years, 1 month ago

Thanks for this snippet. When I add a WMS in the project layers, your sample is not working anymore. I've already posted samples on the qgis-users mailing list to illustrate the issue http://osdir.com/ml/qgis-user-gis/2015-10/msg00096.html . Any clue is welcome.

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional

required

Have a question? Get in touch!