I am working in c++ qt4.8.5 and api qgis 2.8.2 c++. In my application I had to put polygon to define attractive zone. I found a great post in stackexchange (86812). I have based my on this post and on the pyqgis cookbook, but in c++ it seems that it is more difficult. I create a layer in memory with a polygon type, then i create a function to generate ( for the moment ) a random polygon shape with 10 points. There is no error in the build, when i call the function there is nothing on the display. I check the coordinate of the point it is ok.
here is my code
create the polygon layer :
QgsVectorLayer * lVectorLayer = new QgsVectorLayer("Polygon",pType,"memory");
lVectorLayer->isValid();
qDebug() << "Vector valid :" << lVectorLayer->isValid();
// add to the map
QgsMapLayerRegistry::instance()->addMapLayer(lVectorLayer);
// insert the vector layer into the vectorlayermap
mMapLayer.insert(pType, lVectorLayer);
// Add the Layer to the Layer Set
QgsMapCanvasLayer PolygonLayer = QgsMapCanvasLayer(lVectorLayer, TRUE);
mpS57->myListForPrint.insert(1,PolygonLayer);
// Set the Map Canvas Layer Set
mpMapCanvas->setLayerSet(mpS57->myListForPrint);
then the function to create the random polygon :
QgsVectorLayer * lLayer = mMapLayer.value(pType);
if (lLayer == NULL) qDebug() << " lLayer NULL";
else qDebug() << "lLayer Not NULL";
qDebug() << "Vector is Valid ? ::" << lLayer->isValid();
QgsVectorDataProvider * lDataProvider = lLayer->dataProvider();
QgsFeature MyPoly = QgsFeature();
QgsPoint lPoint2;
QgsPolyline lpolyline;
QgsPolygon lPolygon;
// origin point
QgsPoint lOrgPoint;
QgsPoint lPoint1;
PosX =(qrand()%(Xmax-Xmin)+Xmin);
PosY =(qrand()%(Ymin-Ymax)+Ymax);
PosX=PosX / 100000;
PosY=PosY/10000;
lOrgPoint = QgsPoint(PosX,PosY);
lPoint1 = lOrgPoint;
// add 10 points
for (int i=0; i<10; i++) {
//calc new random X and Y
PosX =(qrand()%(Xmax-Xmin)+Xmin);
PosY =(qrand()%(Ymin-Ymax)+Ymax);
PosX=PosX / 100000;
PosY=PosY/10000;
lPoint2=QgsPoint(PosX,PosY);
// create the line
lpolyline.append(lPoint1);
lpolyline.append(lPoint2);
// insert the line in polygon
lPolygon.append(lpolyline);
// clear the line
lpolyline.clear();
//replace lpoint1 by lpoint2
lPoint1 = lPoint2;
}
// close the polygon
// create the line
lpolyline.append(lPoint1);
lpolyline.append(lOrgPoint);
// insert the line in polygon
lPolygon.append(lpolyline);
// clear the line
lpolyline.clear();
MyPoly.setGeometry(QgsGeometry::fromPolygon(lPolygon));
QgsFeatureList lPolyList;
lPolyList << MyPoly;
lDataProvider->addFeatures(lPolyList);
lLayer->updateExtents();
QgsMapLayerRegistry::instance()->addMapLayer(lLayer);
as you can see, in c++ is more complex than in python i try to put a list of point in the frompolygon(listofpoint) but there is an building error in c++ we had to respect the type, in this a QgsPolyline.
I don't understand why tehre is nothing on the display there is the update, the layer is added to the map, the feature is added to the dataprovider, everything seems to be right but nothing.
any idea ?
added information 09-10 : I have made other test with a simple line, same result, but with the same list of point if I use the qgsrubberband, the rubberband is displayed fine on the map, the test with the rubberband confirm that the list of point are correct.