I'm trying to add a QGis map in Qt/C++ widget using QgsMapCanvas, but I have some problems...
I'm working with the following environment: - Ubuntu 12.04 - QGis 2.8.1 - Qt Creator 2.4.1 (based on Qt 4.8.0 - 32 bit)
I found online a sample code that should do what I'm looking for, but something doesn't work..
The code is the following:
int main(int argc, char **argv) { QgsApplication app(argc, argv, true); QString myPluginsDir = "/home/pesce/qgis"; QString myLayerPath = "/home/pesce/qgis_sample_data/shapefiles/alaska.shp"; QString myLayerBaseName = "al"; QString myProviderName = "ogr"; // Instantiate Provider Registry QgsProviderRegistry * preg = QgsProviderRegistry::instance(myPluginsDir); QgsVectorLayer *mypLayer = new QgsVectorLayer(myLayerPath,myLayerBaseName,myProviderName); QgsSingleSymbolRendererV2 *mypRenderer = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer->geometryType())); QList myLayerSet; mypLayer->setRendererV2(mypRenderer); if(mypLayer->isValid()){ qDebug("Layer is valid"); } else { qDebug("Layer is NOT valid"); } // Add the Vector Layer to the Layer Registry QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); // Add the Layer to the Layer Set myLayerSet.append(QgsMapCanvasLayer(mypLayer, TRUE)); // Create the Map Canvas QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0); mypMapCanvas->setExtent(mypLayer->extent()); mypMapCanvas->enableAntiAliasing(true); mypMapCanvas->setCanvasColor(QColor(255, 255, 255)); mypMapCanvas->freeze(false); // Set the Map Canvas Layer Set mypMapCanvas->setLayerSet(myLayerSet); mypMapCanvas->setVisible(true); mypMapCanvas->refresh(); return app.exec(); } The code is correctly compiled and the program correctly starts, but the map is empty..nothing is show... the selected layer (/home/pesce/qgis_sample_data/shapefiles/alaska.shp) seems to be not valid (mypLayer->isValid() returns "false")... but it's a .shp file....it should be valid!
Which kind of files are supported? Is something wrong in the code?
Thanks a lot!
أكثر...
I'm working with the following environment: - Ubuntu 12.04 - QGis 2.8.1 - Qt Creator 2.4.1 (based on Qt 4.8.0 - 32 bit)
I found online a sample code that should do what I'm looking for, but something doesn't work..
The code is the following:
int main(int argc, char **argv) { QgsApplication app(argc, argv, true); QString myPluginsDir = "/home/pesce/qgis"; QString myLayerPath = "/home/pesce/qgis_sample_data/shapefiles/alaska.shp"; QString myLayerBaseName = "al"; QString myProviderName = "ogr"; // Instantiate Provider Registry QgsProviderRegistry * preg = QgsProviderRegistry::instance(myPluginsDir); QgsVectorLayer *mypLayer = new QgsVectorLayer(myLayerPath,myLayerBaseName,myProviderName); QgsSingleSymbolRendererV2 *mypRenderer = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer->geometryType())); QList myLayerSet; mypLayer->setRendererV2(mypRenderer); if(mypLayer->isValid()){ qDebug("Layer is valid"); } else { qDebug("Layer is NOT valid"); } // Add the Vector Layer to the Layer Registry QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); // Add the Layer to the Layer Set myLayerSet.append(QgsMapCanvasLayer(mypLayer, TRUE)); // Create the Map Canvas QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0); mypMapCanvas->setExtent(mypLayer->extent()); mypMapCanvas->enableAntiAliasing(true); mypMapCanvas->setCanvasColor(QColor(255, 255, 255)); mypMapCanvas->freeze(false); // Set the Map Canvas Layer Set mypMapCanvas->setLayerSet(myLayerSet); mypMapCanvas->setVisible(true); mypMapCanvas->refresh(); return app.exec(); } The code is correctly compiled and the program correctly starts, but the map is empty..nothing is show... the selected layer (/home/pesce/qgis_sample_data/shapefiles/alaska.shp) seems to be not valid (mypLayer->isValid() returns "false")... but it's a .shp file....it should be valid!
Which kind of files are supported? Is something wrong in the code?
Thanks a lot!
أكثر...