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(), myRasterFileInfo.completeBaseName()); if (mypLayer->isValid()) { qDebug("Layer is valid"); } else { qDebug("Layer is NOT valid"); return; } // render strategy for grayscale image (will be rendered as pseudocolor) mypLayer->setDrawingStyle( QgsRasterLayer::SingleBandPseudoColor ); mypLayer->setColorShadingAlgorithm( QgsRasterLayer::PseudoColorShader ); mypLayer->setContrastEnhancementAlgorithm( QgsContrastEnhancement::StretchToMinimumMaximum, false ); mypLayer->setMinimumValue( mypLayer->grayBandName(), 0.0, false ); mypLayer->setMaximumValue( mypLayer->grayBandName(), 10.0 ); // Add the Vector Layer to the Layer Registry QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); //create a layerset QList<QgsMapCanvasLayer> myList; // Add the layers to the Layer Set myList.append(QgsMapCanvasLayer(mypLayer, TRUE));//bool visibility // set the canvas to the extent of our layer mpMapCanvas->setExtent(mypLayer->extent()); // Set the Map Canvas Layer Set mpMapCanvas->setLayerSet(myList); }