Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I am trying to add a raster layer, I get this error from QgsRasterLayer::IsValid()!!! Can anybody tell me why this happens!? I am beginner in qgis, so could you please explain in detail. the first func "onAddLayer" used for Add button clicked and second func is defined for adding a raster layer.

Note: I am using QGis 2.18 and gdal 2.3.2 in fedora.

What I have tried:

void  MainWindow::onAddLayer()
  const auto  path = QFileDialog::getOpenFileName(this, "SELECT GIS MAP LAYER", QDir(

  if (path.isEmpty() || path.isNull())

  _lastMapDir = QDir(path).canonicalPath();

void  MainWindow::addRasterLayer(const QFileInfo &info)
  LOG(QString("adding map layer %1").arg(info.filePath()));
  QString  errMsg;

//  if (!QgsRasterLayer::isValidRasterFileName(info.filePath(), errMsg))
//  {
//    LOG(errMsg);

//    return;
//  }

  QScopedPointer<QgsRasterLayer>  layer(new QgsRasterLayer(info.filePath(), info.baseName()));

  if (!layer->isValid())
    LOG(QString("failed to add layer (%1)").arg(layer->error().message(


  save("last_layer_path", info.filePath(), "gis");
  _rasterProvider = layer->dataProvider();
  auto  item = QgsMapCanvasLayer(layer.take(), true);
  QgsMapLayerRegistry::instance()->addMapLayer(item.layer(), true);
  _mapCanvas->setLayerSet(QList<QgsMapCanvasLayer>() << item);
  statusBar()->showMessage(QString("%1 LOADED").arg(info.fileName()), 3000);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900