I want to set transparency for already loaded layer by taking it from canvas

QgsVectorLayer *myLayer = mpMapcanvas.layer(0);

but get an error

invalid conversion from ‘QgsMapLayer*’ to ‘QgsVectorLayer*'

At the same time similat code at python console works great. What I'm doing wrong? Or are there any methods to convert QgsMapLayer into QgsVectorLayer?


I would recommend to take some C++ programming classes, Python code cannot be copied 1:1.

Here's how to cast the layer:

QgsVectorLayer *myLayer = qobject_cast<QgsVectorLayer *>( mpMapcanvas.layer( 0 ) );

And while totally unrelated: if you start a new project now, better start it on QGIS 3.4 (or later).

  • It worked, thanks. Of course I didn't copied python code into C++ project 1:1. I'm glad to start a project on QGIS 3.4. Is there a way to install it on RHEL 7? – Wolf Schulz Mar 6 at 14:37
  • Best I can find is this discussion osgeo-org.1560.x6.nabble.com/… – Matthias Kuhn Mar 6 at 14:49

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.