From 30fa211a2231c54c11ccab828d5b9d8b419a2761 Mon Sep 17 00:00:00 2001 From: tangchao0503 <735056338@qq.com> Date: Mon, 16 Mar 2026 15:06:30 +0800 Subject: [PATCH] =?UTF-8?q?tab=E5=88=87=E6=8D=A2=EF=BC=8C=E6=94=B9?= =?UTF-8?q?=E5=8F=98LayerTreeModel=E7=9A=84setCurrentIndex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HPPA/HPPA.cpp | 35 +++++++++++++++++++++++++++++++++++ HPPA/MapLayerStore.cpp | 9 +++++++++ HPPA/MapLayerStore.h | 3 +++ 3 files changed, 47 insertions(+) diff --git a/HPPA/HPPA.cpp b/HPPA/HPPA.cpp index 4e0cd37..7d630d6 100644 --- a/HPPA/HPPA.cpp +++ b/HPPA/HPPA.cpp @@ -1334,6 +1334,41 @@ void HPPA::onTabWidgetCurrentChanged(int index)//代码新建一个tab,会调 { canvas->setMapTool(activeTool); } + + // Sync layer tree view selection with the current tab + if (m_MapLayerStore && m_layerTreeView && m_LayerTreeModel && m_RasterGroup) + { + MapLayer* layer = m_MapLayerStore->layerForWidget(currentWidget); + if (layer) + { + // Find the LayerTreeLayer node in m_RasterGroup that matches this layer + for (int i = 0; i < m_RasterGroup->childCount(); ++i) + { + LayerTreeNode* child = m_RasterGroup->childAt(i); + if (child && child->type() == LayerTreeNode::Type::Layer) + { + auto* layerNode = static_cast(child); + if (layerNode->mapLayer() == layer) + { + QModelIndex nodeIndex = m_LayerTreeModel->index(i, 0, + m_LayerTreeModel->index(m_RasterGroup->rowInParent(), 0)); + // Block signals to avoid infinite loop with onLayerTreeSelectionChanged + m_layerTreeView->selectionModel()->blockSignals(true); + m_layerTreeView->setCurrentIndex(nodeIndex); + m_layerTreeView->selectionModel()->blockSignals(false); + + // Manually update ImageControl since we blocked the signal + RasterLayer* rl = qobject_cast(layer); + if (rl) + { + m_ic->setActiveLayer(rl); + } + break; + } + } + } + } + } } void HPPA::onActionOpenDirectory() diff --git a/HPPA/MapLayerStore.cpp b/HPPA/MapLayerStore.cpp index 26f7437..65ed3ad 100644 --- a/HPPA/MapLayerStore.cpp +++ b/HPPA/MapLayerStore.cpp @@ -81,3 +81,12 @@ QWidget* MapLayerStore::widgetForLayer(const QString& absolutePath) const } return nullptr; } + +MapLayer* MapLayerStore::layerForWidget(QWidget* widget) const +{ + if (!widget) return nullptr; + for (const auto& kv : m_layerWidgets) { + if (kv.second == widget) return kv.first; + } + return nullptr; +} diff --git a/HPPA/MapLayerStore.h b/HPPA/MapLayerStore.h index 76f5ee1..c535fa8 100644 --- a/HPPA/MapLayerStore.h +++ b/HPPA/MapLayerStore.h @@ -35,6 +35,9 @@ public slots: // Get associated widget by layer absolute data path QWidget* widgetForLayer(const QString& absolutePath) const; + // Reverse lookup: find the MapLayer associated with a given widget (or nullptr) + MapLayer* layerForWidget(QWidget* widget) const; + signals: void layerAdded(MapLayer* layer); // Emitted just before the layer is destroyed/removed from store