Skip to main content

I am trying to change the default symbol for the point/line/ polygon vector layer using QGIS3 C++ APIs. I

I was able to render a vector layer with default symbols on QgsmapcanvasQgsMapCanvas. whenWhen I try to changeschange the default symbol of the point vector layer, somehow it is not changing. whenWhen I am trying to change color, it does not change color as expected, but the symbol color changes in a different color. Here is the piece of code.

        layer->startEditing();     
        qDebug()<< "is editable " << layer->isEditable();  // returns true
        QgsFeatureRenderer * layerRenderer= layer->renderer();
        QgsSingleSymbolRenderer *mSingleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
        QgsMarkerSymbol* symbol = new  QgsMarkerSymbol();
        QgsStringMap mp;
        mp[QString("name")]= QString("square");
        mp[QString("color")]=QString("yellow");
        symbol->createSimple(mp);
        symbol->setColor(QColor(255,255,255));
        mSingleRenderer->setSymbol(symbol);
        layer->triggerRepaint();
        mLayerTreeView->refreshLayerSymbology(layer->id());
        layer->commitChanges();
      
variable-name layer defines Qgsvectorlayer. 

variable-name layer defines QgsVectorLayer.

I am trying to change the default symbol for the point/line/ polygon vector layer using QGIS3 C++ APIs. I was able to render a vector layer with default symbols on Qgsmapcanvas. when I try to changes the default symbol of the point vector layer, somehow it is not changing. when I am trying to change color, it does not change color as expected, but the symbol color changes in a different color. Here is the piece of code.

        layer->startEditing();     
        qDebug()<< "is editable " << layer->isEditable();  // returns true
        QgsFeatureRenderer * layerRenderer= layer->renderer();
        QgsSingleSymbolRenderer *mSingleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
        QgsMarkerSymbol* symbol = new  QgsMarkerSymbol();
        QgsStringMap mp;
        mp[QString("name")]= QString("square");
        mp[QString("color")]=QString("yellow");
        symbol->createSimple(mp);
        symbol->setColor(QColor(255,255,255));
        mSingleRenderer->setSymbol(symbol);
        layer->triggerRepaint();
        mLayerTreeView->refreshLayerSymbology(layer->id());
        layer->commitChanges();
      
variable-name layer defines Qgsvectorlayer. 

I am trying to change the default symbol for the point/line/ polygon vector layer using QGIS3 C++ APIs.

I was able to render a vector layer with default symbols on QgsMapCanvas. When I try to change the default symbol of the point vector layer, somehow it is not changing. When I am trying to change color, it does not change color as expected, but the symbol color changes in a different color. Here is the piece of code.

layer->startEditing();     
qDebug()<< "is editable " << layer->isEditable();  // returns true
QgsFeatureRenderer * layerRenderer= layer->renderer();
QgsSingleSymbolRenderer *mSingleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
QgsMarkerSymbol* symbol = new  QgsMarkerSymbol();
QgsStringMap mp;
mp[QString("name")]= QString("square");
mp[QString("color")]=QString("yellow");
symbol->createSimple(mp);
symbol->setColor(QColor(255,255,255));
mSingleRenderer->setSymbol(symbol);
layer->triggerRepaint();
mLayerTreeView->refreshLayerSymbology(layer->id());
layer->commitChanges();

variable-name layer defines QgsVectorLayer.

Source Link

Changing symbol and color of vector Layer using QGIS 3 C++ APIs

I am trying to change the default symbol for the point/line/ polygon vector layer using QGIS3 C++ APIs. I was able to render a vector layer with default symbols on Qgsmapcanvas. when I try to changes the default symbol of the point vector layer, somehow it is not changing. when I am trying to change color, it does not change color as expected, but the symbol color changes in a different color. Here is the piece of code.

        layer->startEditing();     
        qDebug()<< "is editable " << layer->isEditable();  // returns true
        QgsFeatureRenderer * layerRenderer= layer->renderer();
        QgsSingleSymbolRenderer *mSingleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
        QgsMarkerSymbol* symbol = new  QgsMarkerSymbol();
        QgsStringMap mp;
        mp[QString("name")]= QString("square");
        mp[QString("color")]=QString("yellow");
        symbol->createSimple(mp);
        symbol->setColor(QColor(255,255,255));
        mSingleRenderer->setSymbol(symbol);
        layer->triggerRepaint();
        mLayerTreeView->refreshLayerSymbology(layer->id());
        layer->commitChanges();
      
variable-name layer defines Qgsvectorlayer.