In the project I'm working on (C/C++/Qt application), we are trying to integrate QGIS (latest version desirable, 2.4 for the moment). But there is very little information in the web on how to use QGIS C++ API.
To start I wanted to write a simple code example (read shapefile and visualize it in a window). I found a code example for QGIS 1.8, but it does not work with QGIS 2.4 as the API has changed since. Then I tried to edit it in order to make it work with QGIS 2.4, but it did not succeed. Here is the original code:
#include <QtCore/QString>
#include <QtGui/QApplication>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
#include <iostream>
int main(int argc, char** argv)
{
// Creation of the Qt GIS application
QgsApplication app(argc, argv, true);
// Hard coded paths
QString myPluginsDir = "/usr/lib64/qgis";
QString myLayerPath = "./HelloWorld/GUI/data/helloQGIS.shp";
QString myLayerBaseName = "helloQGIS";
QString myProviderName = "ogr";
// Instantiate Provider Registry
QgsProviderRegistry::instance(myPluginsDir);
// Create a maplayer instance
QgsVectorLayer* mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName);
QgsSingleSymbolRenderer* mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType());
QList <QgsMapCanvasLayer> myLayerSet;
mypLayer->setRenderer(mypRenderer);
if (mypLayer->isValid())
{
qDebug("Layer is valid");
}
else
{
qDebug("Layer is NOT valid");
}
// Add the Vector Layer to the Layer Registry
QList<QgsMapLayer*> theMapLayers;
theMapLayers.append(mypLayer);
QgsMapLayerRegistry::instance()->addMapLayers(theMapLayers, 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();
mypMapCanvas->show();
// Start the Application Event Loop
return app.exec();
}
I've tried many different ways to modify this code in order to make it work with QGIS 2.4, but no success. The only source of information I've used is the official doc API.
I said to myself that maybe someone has already did it and/or have any other code example of using QGIS 2.4. As GIS is a new domain for me, I have some difficulties in understanding how the API should work.