Skip to content
Permalink
Browse files

Fix emitting of currentLayerChanged() signal

Use layer ID instead of persistent index to remember last layer.
Persistent index will get invalid during removal of layers and
the signal was not emitted when current index got invalid (none).

This was causing a crash in GPS information widget
  • Loading branch information
wonder-sk committed Sep 10, 2014
1 parent 7fe45db commit 4ef67ca449e8bd23119231007771e4f08479f65b
Showing with 13 additions and 6 deletions.
  1. +11 −4 src/gui/layertree/qgslayertreeview.cpp
  2. +2 −2 src/gui/layertree/qgslayertreeview.h
@@ -158,13 +158,20 @@ void QgsLayerTreeView::updateExpandedStateToNode( QModelIndex index )
void QgsLayerTreeView::onCurrentChanged()
{
QgsMapLayer* layerCurrent = layerForIndex( currentIndex() );
QModelIndex layerCurrentIndex = layerCurrent ? layerTreeModel()->node2index( layerTreeModel()->rootGroup()->findLayer( layerCurrent->id() ) ) : QModelIndex();
if ( mCurrentIndex == layerCurrentIndex )
QString layerCurrentID = layerCurrent ? layerCurrent->id() : QString();
if ( mCurrentLayerID == layerCurrentID )
return;

layerTreeModel()->setCurrentIndex( layerCurrentIndex );
// update the current index in model (the item will be underlined)
QModelIndex nodeLayerIndex;
if ( layerCurrent )
{
QgsLayerTreeLayer* nodeLayer = layerTreeModel()->rootGroup()->findLayer( layerCurrentID );
nodeLayerIndex = layerTreeModel()->node2index( nodeLayer );
}
layerTreeModel()->setCurrentIndex( nodeLayerIndex );

mCurrentIndex = layerCurrentIndex;
mCurrentLayerID = layerCurrentID;
emit currentLayerChanged( layerCurrent );
}

@@ -112,8 +112,8 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
QgsLayerTreeViewDefaultActions* mDefaultActions;
//! Context menu provider. Owned by the view.
QgsLayerTreeViewMenuProvider* mMenuProvider;
//! Keeps track of current index (to check when to emit signal about change of current layer)
QPersistentModelIndex mCurrentIndex;
//! Keeps track of current layer ID (to check when to emit signal about change of current layer)
QString mCurrentLayerID;
};


0 comments on commit 4ef67ca

Please sign in to comment.
You can’t perform that action at this time.