Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am confused a bit, using addVectorLayer() in QGIS plugin.

I want to add and display a vector layer, directly when QGIS opens. Hence I am using addVectorLayer() in _init_() function of my plugin. The problem I am seeing is, the vector layer is loaded in the legend but is not displayed on the canvas. I, manually have to click on "zoom to layer extent", to display it.

Any hints on how to solve this problem will be of high help to me.

NOTE: If I use either of the below, it works perfect (i.e. the layer is loaded and displayed as well).

  1. qgis.exe D:/sample.shp at the command prompt OR
  2. In QGIS Plugin, If I use addVectorLayer() on some menu item click
share|improve this question
Have you tried in the initGui method() – Nathan W Jul 26 '12 at 11:16
Yes, I have tried putting that in initGui() function too. It does not seem to work..... If I unload the plugin and reload it, then it works fine. – SDH Jul 26 '12 at 13:44
Have you tried a singleshot QTimer? It is a common technique for running actions once the event loop is ready. I believe setting it to 0 just queues your timer action. – dakcarto Jul 27 '12 at 0:41
oh great !!... Thanks Dakcarto... that worked (using 0 still does not display the layer. Only 280ms & above helps). But, I wonder why QGIS doesnt display without using any trick !! i.e without a mouse click or timers, etc, etc ? – SDH Jul 27 '12 at 5:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.