[Qgis-user] Fail to show markers in a vector layer
Hi, I'm Simone Melis and I'm sw engineer in an Italian company. I' m using QGis API 3.26 on Ubuntu 20.04, to create an application GUI using Qt 5.15.2 I created a vector layer and I can show correctly a georeferenced pdf image. Then I added another transparent layer (vector layer) and I tried to add markers in lat,long points. Using QgsSimpleMarkerSymbolLayer I can inserted correctly all markers of the same shape and color. Then I tried to add markers with different shapes and colors on the same layers; using QgsSimpleMarkerSymbolLayer I show only one kind of marker. Browsing on internet I found an example to use QgsCategorizedSymbolRenderer. I tried to implement it in my code but no markers shown. Can you suggest a correct way to do this? Thanks, Simone Here code I implemented: doublelatitude[4]={44.095003,44.095103,44.095203,44.094811}; doublelongitude[4]={9.858660,9.859060,9.859460,9.859034}; // //QgsCategorizedSymbolRenderer // transparentLayerBSPRX->dataProvider()->addAttributes({QgsField("Category",QVariant::String), QgsField("Name",QVariant::String), QgsField("Latitude",QVariant::Double), QgsField("Longitude",QVariant::Double), QgsField("Type",QVariant::String)}); //Setupthedefaultsymbol QgsSimpleMarkerSymbolLayer*BSPRX_SymbolLayer=newQgsSimpleMarkerSymbolLayer(); BSPRX_SymbolLayer->setShape(QgsSimpleMarkerSymbolLayer::Circle); BSPRX_SymbolLayer->setSize(5.0); BSPRX_SymbolLayer->setColor(Qt::blue); QgsSymbol*def_symbol=QgsSymbol::defaultSymbol(QgsWkbTypes::PointGeometry); def_symbol->changeSymbolLayer(0,BSPRX_SymbolLayer); for(inti=0;i<4;i++)//Correctedloopcondition { QgsFeaturefeature(transparentLayerBSPRX->dataProvider()->fields()); if(i<1) { //Createanewfeature feature.setAttribute("Category","A"); feature.setAttribute("Name","RX"); feature.setAttribute("Latitude",latitude[i]); feature.setAttribute("Longitude",longitude[i]); feature.setAttribute("Type","BSPRX"); def_symbol->symbolLayers()[0]->setColor(Qt::red); } else { //Createanewfeature feature.setAttribute("Category","B"); feature.setAttribute("Name","TX"); feature.setAttribute("Latitude",latitude[i]); feature.setAttribute("Longitude",longitude[i]); feature.setAttribute("Type","BSPTX"); def_symbol->symbolLayers()[0]->setColor(Qt::blue); } //Addthefeaturetothelayer transparentLayerBSPRX->dataProvider()->addFeatures(QgsFeatureList()addCategory(bsprxCategory); QgsRendererCategorybsptxCategory("B",def_symbol->clone(),"TX",true); categorizedRenderer->addCategory(bsptxCategory); //Printsymbolsfordebugging QListcategories=categorizedRenderer->categories(); for(constQgsRendererCategory&category:categories) { qDebug()<<"Category:" setRenderer(categorizedRenderer); -- -- Questa email è stata esaminata alla ricerca di virus dal software antivirus Avast. www.avast.com___ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Fail to show markers in a vector layer
> I created a vector layer and I can show correctly a georeferenced pdf > image. > Then I added another transparent layer (vector layer) and I tried to > add markers in lat,long points. > Using QgsSimpleMarkerSymbolLayer I can inserted correctly all markers of > the same shape and color. Then I tried to add markers with different > shapes and colors on the same layers; using QgsSimpleMarkerSymbolLayer I > show only one kind of marker. Browsing on internet I found an example to > use QgsCategorizedSymbolRenderer. I tried to implement it in my code but > no markers shown. Can you suggest a correct way to do this? You should try to do this through the normal QGIS GUI, then once you have it working in the GUI you will have a better understanding of what your code needs to achieve. I haven't tried to do it in code, but: > def_symbol->symbolLayers()[0]->setColor(Qt::red); > def_symbol->symbolLayers()[0]->setColor(Qt::blue); Here you're just changing settings on the same symbol, so you'll only end up with one symbol. > QgsCategorizedSymbolRenderer*categorizedRenderer=newQgsCategorizedSymbolRenderer("Category"); > QgsRendererCategorybsprxCategory("A",def_symbol->clone(),"RX",true); > categorizedRenderer->addCategory(bsprxCategory); > > QgsRendererCategorybsptxCategory("B",def_symbol->clone(),"TX",true); > categorizedRenderer->addCategory(bsptxCategory); Here you're saying when your Category field is "A", use the same symbol as when your Category field is "B". You'll have to assign different symbols if you want them to appear differently. I guess you want to call def_symbol->clone() first, then change the colour on the clone, before assigning it to Category A or B. Cheers, Adam. ___ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Fail to show markers in a vector layer
Thanks for your answer. Now I can use correctly QgsCategorizedSymbolRenderer and show markers. Now I've another question. I would like put near each marker a label. I can do it in QGIS Desktop 3.26 in section Labels for my vector layer. Can you suggest a way to do this using qgsi api c++ (Qt 5.15.2 in ubuntu 20.04) Regards, Simone Il 13/01/2024 12:03, Adam Nielsen ha scritto: I created a vector layer and I can show correctly a georeferenced pdf image. Then I added another transparent layer (vector layer) and I tried to add markers in lat,long points. Using QgsSimpleMarkerSymbolLayer I can inserted correctly all markers of the same shape and color. Then I tried to add markers with different shapes and colors on the same layers; using QgsSimpleMarkerSymbolLayer I show only one kind of marker. Browsing on internet I found an example to use QgsCategorizedSymbolRenderer. I tried to implement it in my code but no markers shown. Can you suggest a correct way to do this? You should try to do this through the normal QGIS GUI, then once you have it working in the GUI you will have a better understanding of what your code needs to achieve. I haven't tried to do it in code, but: def_symbol->symbolLayers()[0]->setColor(Qt::red); def_symbol->symbolLayers()[0]->setColor(Qt::blue); Here you're just changing settings on the same symbol, so you'll only end up with one symbol. QgsCategorizedSymbolRenderer*categorizedRenderer=newQgsCategorizedSymbolRenderer("Category"); QgsRendererCategorybsprxCategory("A",def_symbol->clone(),"RX",true); categorizedRenderer->addCategory(bsprxCategory); QgsRendererCategorybsptxCategory("B",def_symbol->clone(),"TX",true); categorizedRenderer->addCategory(bsptxCategory); Here you're saying when your Category field is "A", use the same symbol as when your Category field is "B". You'll have to assign different symbols if you want them to appear differently. I guess you want to call def_symbol->clone() first, then change the colour on the clone, before assigning it to Category A or B. Cheers, Adam. -- -- Questa email è stata esaminata alla ricerca di virus dal software antivirus Avast. www.avast.com___ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user