I am trying to utilize the C++ code examples from blog.qgis.org, specifically Tutorial 4, Adding Rasters to QGIS Map Canvas.

I am successfully able to run the example loading a GeoTIFF with QGIS 1.4, however with 1.8 I get "Layer is Not Valid". As a note, the same GeoTIFF file was able to be loaded in the QGIS 1.8 application just fine, but not in the example code. I've tried with several different files, with the same result. Is there additions to the 1.8 API which require additional calls to get this functionality to work? Problem code below. The full example code can be found at http://blog.qgis.org/node/94

Operating System: Fedora 17 64-bit, QGIS 1.8.0

void MainWindow::addLayer()
  QString myFileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                         QCoreApplication::applicationDirPath () + "/data",
                         tr("GeoTiff (*.tif)"));
  QFileInfo myRasterFileInfo(myFileName);
  QgsRasterLayer * mypLayer = new QgsRasterLayer(myRasterFileInfo.filePath(), 
  if (mypLayer->isValid())
     qDebug("Layer is valid");
     qDebug("Layer is NOT valid");

1 Answer 1


The API is the same even in master and the test cases use almost the same code. If it is a standalone app, you likely need to set up some data paths, so QGIS can find the bundled CRS info ... Try adding this somewhere at the start:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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