I am writing a simple program to display raster data using qgis api C++.
When I open raster data that has different coordinate systems, the on-the-fly feature does not work on my mapCanvas.
Can someone help me?
OpenGisData::OpenGisData(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
QWidget* centralWidget = OpenGisDataClass::centralWidget;
mMapCanvas = new QgsMapCanvas(centralWidget);
QGridLayout* gridLayout = new QGridLayout;
gridLayout->addWidget(mMapCanvas);
centralWidget->setLayout(gridLayout);
connect(actionOpen, &QAction::triggered, this, &OpenGisData::open);
}
void OpenGisData::open()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open data", "D:\\Test");
if (!fileName.isEmpty())
{
QgsRasterLayer* rasterLayer = new QgsRasterLayer(fileName);
if (rasterLayer && rasterLayer->isValid())
{
QList<QgsMapLayer*> layers = mMapCanvas->layers();
layers.append(rasterLayer);
mMapCanvas->setLayers(layers);
mMapCanvas->zoomToFullExtent();
}
}
}