At Manya Technologies, we develop custom GIS tools using the powerful QGIS C++ API, integrated seamlessly into Qt interfaces. One essential feature in any GIS viewer is the ability to manage map layers interactively β that’s where the Layer Tree View comes in.
In this post, we share a working example of how to implement a QgsLayerTreeView alongside a QgsMapCanvas, giving users full control over the layers in a map project β including visibility toggles, drag-and-drop, and renaming.
π§ What is QgsLayerTreeView
?
QgsLayerTreeView
is a Qt widget that shows the hierarchical structure of the map’s layers (similar to the “Layers” panel in QGIS Desktop). It is fully interactive and customizable.
π Complete Working Code Snippet
// Create and set the project instance
mQGisProject = new QgsProject();
QgsProject::setInstance(mQGisProject);
// Create the map canvas
mMapCanvas = new QgsMapCanvas();
mMapCanvas->setProject(mQGisProject);
mMapCanvas->setDestinationCrs(QgsCoordinateReferenceSystem::fromEpsgId(4326));
mMapCanvas->installEventFilter(this);
mMapCanvas->clearExtentHistory(); // Reset zoom next/last
ui->gridLayoutMapCanvas->addWidget(mMapCanvas);
/********** Layer Tree View Setup **********/
// Initialize the tree view widget
mLayerTreeView = new QgsLayerTreeView();
mLayerTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
mLayerTreeView->setDragDropMode(QAbstractItemView::DragDrop);
mLayerTreeView->setAcceptDrops(true);
// Get root of layer tree
mLayerTree = mQGisProject->layerTreeRoot();
// Create a model for the tree view
QgsLayerTreeModel *layerTreeModel = new QgsLayerTreeModel(mQGisProject->layerTreeRoot());
layerTreeModel->setFlag(QgsLayerTreeModel::ShowLegendAsTree, true); // Show legend in tree form
layerTreeModel->setFlag(QgsLayerTreeModel::AllowNodeReorder, true); // Allow dragging layers
layerTreeModel->setFlag(QgsLayerTreeModel::AllowNodeChangeVisibility, true); // Add checkboxes for visibility
layerTreeModel->setFlag(QgsLayerTreeModel::AllowNodeRename); // Allow renaming layers
mLayerTreeView->setModel(layerTreeModel);
// Optional: Add LayerTreeView to a layout in your UI
ui->verticalLayoutSidebar->addWidget(mLayerTreeView); // Adjust this to your layout
// Bridge the layer tree and map canvas
mLayerTreeMapCanvasBridge = new QgsLayerTreeMapCanvasBridge(mQGisProject->layerTreeRoot(), mMapCanvas);
π― Key Features Enabled
- β Toggle layer visibility with checkboxes
- β Drag-and-drop to reorder layers
- β Rename layers in the tree
- β Auto-sync with map canvas
- β Legend symbols displayed as a tree
π‘ Practical Use Case
This setup mimics the QGIS Desktop experience inside a Qt Widget-based GIS viewer, giving users intuitive control over layers β crucial for defense, simulation, and monitoring dashboards.
π Requirements
- QGIS C++ SDK (built with Qt support)
- Qt 5 or Qt 6
- UI with
QGridLayout
andQVBoxLayout
(or any layout container) - One or more loaded layers (vector or raster) for display
π Letβs Build It for You
Want a lightweight, custom-built desktop GIS application that supports full-featured layer management?
We can help.
π§ business@manyatechnologies.com
π www.manyatechnologies.com
#QGIS #Qt #Cplusplus #QgsLayerTreeView #GISApplication #CustomGIS #DesktopGIS #GeospatialTools #MapCanvas #ManyaTechnologies