[Qgis-user] Question

Alireza Ghalavand arghalavand at gmail.com
Tue Jul 21 04:18:17 PDT 2020


Hello,

Qgis Error “Raster layer Cannot instantiate the 'gdal' data provider” in qt
c++
<https://stackoverflow.com/questions/63010283/qgis-error-raster-layer-cannot-instantiate-the-gdal-data-provider-in-qt-c>
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.

*my code:*
void  MainWindow::onAddLayer()
{
  const auto  path = QFileDialog::getOpenFileName(this, "SELECT GIS MAP
LAYER", QDir(
                                                    _lastMapDir).path());

  if (path.isEmpty() || path.isNull())
  {
    return;
  }

  addRasterLayer(path);
  _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()));
  //layer->setDataProvider("gdal"); // Added by ArGhalavand

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

QgsErrorMessage::Format::Text)));

    return;
  }

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

*With Best Regards,*
Alireza Ghalavand
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20200721/b986adb7/attachment.html>


More information about the Qgis-user mailing list