QGis 2.4 C ++ helloWorld
In a project I am working on (C / C ++ / Qt application) we are trying to integrate QGis (latest version preferably 2.4 for now). But there is very little information on the internet on how to use the QGis C ++ API.
To begin with, I wanted to write a simple code example (read a shapefile and render it in a window). I found sample code for QGis 1.8, but it doesn't work with QGis 2.4 as the API has changed since then. Then I tried to edit it to work with QGis 2.4 but failed. Here is the source 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 tried many different ways to change this code to work with QGis 2.4 but failed. The only source of information I have used is the official doc API .
I said myself that maybe someone has already done this and / or has some other example of using QGis 2.4. Since GIS is a new domain for me, I have some difficulties to understand how the API is supposed to work. I appreciate any help, thanks.
source to share
Better to ask QGIS questions on the https://gis.stackexchange.com/ forum . Not sure if there is a way to move this question.
source to share