Hi everyone,

We tried to follow tutorial 1 ("Write a sample mapping application using new
QGIS Canvas API") and tutorial 2 ("Using Map Tools with the QGIS Canvas API" to
create an application based on QGIS librairies (under Windows, Qt4.2, compil
with mingw). We got problems trying to show not one, but two default layers at
start. Indeed, the program does not take layers identified by
"myLayerBaseName", but the first one according to alphabetical order (in
"myLayerPath"). The path "myLayerPath" is the right one, tough. We tried many
changes to that code, but none gave us the answer.

Someone got a cue?
Thanks in advance.



//
// QGIS Includes
//
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
//
// Qt Includes
//
#include <QString>
#include <QApplication>
#include <QWidget>
//
// Std Includes
//
#include <deque.h>
#include <iostream>

int main(int argc, char ** argv)
{
  // Start the Application
  QgsApplication app(argc, argv, true);

  QString myPluginsDir        =
"C:\\qgis\\bolduc\\qgis\\qgis-0.8.0-Preview-2-bin-win32\\lib\\qgis";
  QString myLayerPath         = "data";
  QString myLayerBaseName     = "test";
  QString myProviderName      = "ogr";

// Instantiate Provider Registry
  QgsProviderRegistry::instance(myPluginsDir);

QgsVectorLayer * mypLayer =
      new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName);
  QgsSingleSymbolRenderer *mypRenderer = new
QgsSingleSymbolRenderer(mypLayer->vectorType());
  std::deque<QString> 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
  QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE);
  // Add the Layer to the Layer Set
  myLayerSet.push_back(mypLayer->getLayerID());
  mypLayer->setVisible(TRUE);

// Create the Map Canvas
  QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0);
  mypMapCanvas->setExtent(mypLayer->extent());
  qDebug(mypMapCanvas->extent().stringRep(2));
  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();

// Start the Application Event Loop
  return app.exec();
}


_______________________________________________
Qgis-user mailing list
Qgis-user@lists.qgis.org
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-user

Reply via email to