Building a QGIS Map Viewer Application Using Qt & C++

Custom GIS and Simulation Services - Manya Technologies

At Manya Technologies, we develop custom GIS applications using QGIS API, Qt, and C++, tailored for real-time data visualization and geospatial intelligence. In this post, we’re sharing a working code snippet that demonstrates how to build a basic map viewer capable of loading and displaying shapefiles using the QGIS C++ API.


πŸš€ What This Code Does

  • Initializes a QGIS project in Qt
  • Embeds a QgsMapCanvas into a Qt GUI (QGridLayout)
  • Loads a shapefile (Borders.shp)
  • Sets up the map view with CRS, layer visibility, and extent

🧩 Core Code Snippet

cppCopyEdit// Initialize QGIS project and canvas
mQGisProject = new QgsProject();
QgsProject::setInstance(mQGisProject);

mMapCanvas = new QgsMapCanvas();
mMapCanvas->setProject(mQGisProject);
mMapCanvas->setDestinationCrs(QgsCoordinateReferenceSystem::fromEpsgId(4326));
mMapCanvas->installEventFilter(this);
mMapCanvas->clearExtentHistory(); // Reset zoom next/last

// Add canvas to layout
ui->gridLayoutMapCanvas->addWidget(mMapCanvas);

πŸ“Œ Adding a Vector Layer Dynamically

cppCopyEditvoid MainWindow::addLayer(){
QString strLayerPath(SHAPE_FILE_PATH);
strLayerPath.append("shapefile1.shp");

QgsVectorLayer *vectorLayer = new QgsVectorLayer(strLayerPath, QFileInfo(strLayerPath).fileName(), "ogr");

if(vectorLayer->isValid())
{
mLayers.append(vectorLayer);
mMapCanvas->setExtent(vectorLayer->extent());
mQGisProject->addMapLayer(vectorLayer);
mMapCanvas->setLayers(mLayers);
mMapCanvas->refresh();
}
else{
qDebug() << "Layer is not valid: " << vectorLayer->error().message();
}
}

πŸ”§ Requirements

  • QGIS built with C++ bindings
  • Qt 6 or Qt 5
  • Linked libraries: qgis_core, qgis_gui, gdal, proj, etc.
  • Shapefile at SHAPE_FILE_PATH location

βœ… Result

Once compiled and run, the application:

  • Loads a shapefile (Borders.shp)
  • Displays it on an interactive map canvas
  • Supports panning, zooming, and layer control

This forms the base framework for building advanced GIS tools such as:

  • Real-time track display
  • Vector/raster overlays
  • Symbology and temporal layer support
  • Custom plugins and data editing

πŸ”— Use Case: Lightweight Desktop GIS Viewer

This is perfect for:

  • Custom dashboards
  • Tactical planning tools
  • Sensor data overlays
  • Offline GIS apps for field operations

πŸ“ž Want to build a custom GIS viewer?

Let’s help you create scalable, cross-platform GIS applications with native performance.

πŸ“§ business@manyatechnologies.com
🌐 www.manyatechnologies.com


#Qt #QGIS #GISViewer #Cplusplus #CustomGIS #MapDisplay #Shapefile #GeospatialTech #QgsMapCanvas #ManyaTechnologies

Scroll to Top