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
  • 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
  • 1
    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
  • Please write the solution as an answer. Thank you. – Afshin Salehi Feb 15 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.