diff --git a/src/app/qgssnappinglayertreemodel.cpp b/src/app/qgssnappinglayertreemodel.cpp index cd18fa3a05ce..5d066b11d63a 100644 --- a/src/app/qgssnappinglayertreemodel.cpp +++ b/src/app/qgssnappinglayertreemodel.cpp @@ -409,8 +409,33 @@ QgsLayerTreeModel *QgsSnappingLayerTreeModel::layerTreeModel() const void QgsSnappingLayerTreeModel::setLayerTreeModel( QgsLayerTreeModel *layerTreeModel ) { + if ( mLayerTreeModel ) + { + disconnect( mLayerTreeModel, &QAbstractItemModel::rowsInserted, this, &QgsSnappingLayerTreeModel::onNodesInserted ); + disconnect( mLayerTreeModel, &QAbstractItemModel::rowsRemoved, this, &QgsSnappingLayerTreeModel::onNodesRemoved ); + } + mLayerTreeModel = layerTreeModel; QSortFilterProxyModel::setSourceModel( layerTreeModel ); + + connect( mLayerTreeModel, &QAbstractItemModel::rowsInserted, this, &QgsSnappingLayerTreeModel::onNodesInserted ); + connect( mLayerTreeModel, &QAbstractItemModel::rowsRemoved, this, &QgsSnappingLayerTreeModel::onNodesRemoved ); +} + +void QgsSnappingLayerTreeModel::onNodesInserted( const QModelIndex &parent, int first, int last ) +{ + if ( mLayerTreeModel->rowCount( parent ) - ( last - first + 1 ) <= 0 ) + { + invalidateFilter(); + } +} + +void QgsSnappingLayerTreeModel::onNodesRemoved( const QModelIndex &parent, int, int ) +{ + if ( mLayerTreeModel->rowCount( parent ) == 0 ) + { + invalidateFilter(); + } } bool QgsSnappingLayerTreeModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const diff --git a/src/app/qgssnappinglayertreemodel.h b/src/app/qgssnappinglayertreemodel.h index af0fad3fe7ee..fba8bb70f3bf 100644 --- a/src/app/qgssnappinglayertreemodel.h +++ b/src/app/qgssnappinglayertreemodel.h @@ -91,6 +91,8 @@ class APP_EXPORT QgsSnappingLayerTreeModel : public QSortFilterProxyModel void onSnappingSettingsChanged(); private: + void onNodesInserted( const QModelIndex &parent, int first, int last ); + void onNodesRemoved( const QModelIndex &parent, int first, int last ); bool nodeShown( QgsLayerTreeNode *node ) const; QgsProject *mProject = nullptr;