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