//4th 代码
#include
#include
//4th 代码
QFileInfo myRasterFileInfo("../data/Abarema_jupunba_projection.tif");
QgsRasterLayer *mypLayer = new QgsRasterLayer(myRasterFileInfo.filePath(),myRasterFileInfo.completeBaseName());
if (mypLayer->isValid())
{
QMessageBox::information(NULL,QString(tr("加载图层")),QString(tr("加载成功")),QMessageBox::Yes|QMessageBox::No);
mypLayer->setDrawingStyle(QgsRasterLayer::SingleBandPseudoColor);
mypLayer->setColorShadingAlgorithm(QgsRasterLayer::PseudoColorShader);
mypLayer->setContrastEnhancementAlgorithm(QgsContrastEnhancement::StretchToMinimumMaximum,false);
QList myLayerSet;
QgsMapLayerRegistry::instance()->addMapLayer(mypLayer,TRUE);
mpMapCanvas->setExtent(mypLayer->extent());
myLayerSet.append(QgsMapCanvasLayer(mypLayer));
mpMapCanvas->setLayerSet(myLayerSet);
}
else
{
QMessageBox::information(NULL,QString(tr("加载图层")),QString(tr("加载失败")),QMessageBox::Yes|QMessageBox::No );
}
备注:(1)baseName和completeBaseName的区别:baseName返回的字符不包含“.”;而completeBaseName返回的字符不包含最后一个“.”之后的字符。
QgsApplication a(argc, argv,TRUE);
QTextCodec *codec = QTextCodec::codecForName("System");
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForTr(codec);
qgis2 w;
w.show();
return a.exec()