6

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.

5

I had your same problem. I solved it updating the code in order to match the QGis 2 major changes:

int main(int argc, char *argv[])
{
QgsApplication app(argc, argv, true);

QString myPluginsDir        = "/usr/lib64/qgis";
QString myLayerPath         = "pathtoashapefile";
QString myLayerBaseName     = "italy";
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 <QgsMapCanvasLayer> 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();

}

0

QGIS (v2) have some change on rendering engine and you must change QgsSingleSymbolRenderer to QgsSingleSymbolRendererV2. The next version of QGIS has same interface like first version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.