In my application I have a mapcanvas that contains several layers. some of layers need to be updated or rendered again because data has been changed ( like the geometry or some attributes )

Is there another way to update only one layer in a mapcanvas ?

My previous search is setLayerSet actually i use the setLayerSet but all of the layer are rendered again,

I am working in c++ / §QT 4.8.5 and qgis api c++ 2.8.2

  • Hi, it seems that you are mixing two questions here. I assume that you are actually interested in re-rendering only a single layer and now have a suspicion that it could be related to setLayerSet? If yes, please reformulate your question (and title) and mention setLayerSet as "previous research". It will help to give you an answer to your real problem. – Matthias Kuhn Sep 7 '15 at 17:47
  • ok i change the title and the question – ainardi Sep 7 '15 at 18:54
  • hum perhaps i found in the topics 66075 something interresting , i will try it tomorrow – ainardi Sep 7 '15 at 19:10
  • in fact in this post (66075) it is only for the order of the rendering, what i need is to re-render only one layer – ainardi Sep 8 '15 at 6:29

What you want to do requires two things to work properly.

  1. You have to enable caching for the map canvas (once, on initialization)
  2. You will have to request a repaint of the layer in question (whenever you want to repaint it)

Enable caching

By default a new QgsMapCanvas will be initialized with caching disabled. To enable it do

mapCanvas->setCachingEnabled( true );

Request a repaint

To request a repaint for a particular layer, you will have to call its triggerRepaint() method. This method is defined on QgsMapLayer level, so it works the same for raster layers.


This method is a slot and therefore can also be connected to a signal if this makes things easier.

connect( myObject, SIGNAL( thingsHaveChanged() ), mapLayer, SLOT( triggerRepaint() ) );
  • for the signal can i use layerschanged() of the class qgsmapcanvas or is it better to use the datachanged() of the class qgsmaplayer – ainardi Sep 9 '15 at 7:53
  • You better provide your own signal which is emitted according to your business logic. Abuse of existing signals always bears the risk of undesired secondary effects. – Matthias Kuhn Sep 9 '15 at 8:20
  • I only put the cache to true and it works !!! my vectorlayer move on the screen without re-render all the canvas and without call the triggerrepaint!!! There is something i don't understand if i need to call triggerrepaint for the layer why does it work without call the function ? doaes the fact that i call setlayerset do the same thing – ainardi Sep 9 '15 at 11:05
  • Yes it does call the same internally, but setLayerSet isn't meant to do this. So it may do other things which you do not need and it is possible that in a future version it will change its behavior (e.g. clear the cache when it's called...). You better switch to calling triggerRepaint and only call setLayerSet when you add or remove a layer. – Matthias Kuhn Sep 9 '15 at 11:19

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.