Adding a Layer Tree View in a QGIS-based Qt Application using C++

Custom GIS and Simulation Services - Manya Technologies

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 and QVBoxLayout (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

Scroll to Top