Skip to content

Commit 7fb4bea

Browse files
committed
Fix map layer combo box sometimes showing a selected layer which
is not applied This could also have been fixed by changing from the activated signal to currentIndexChanged for the indexChanged connection, but it looks like activated was intentionally used here.
1 parent bf25186 commit 7fb4bea

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

python/gui/qgsmaplayercombobox.sip

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ class QgsMapLayerComboBox : QComboBox
2828
//! returns the list of excepted layers
2929
QList<QgsMapLayer*> exceptedLayerList() const;
3030

31-
//! currentLayer returns the current layer selected in the combo box
31+
/** Returns the current layer selected in the combo box.
32+
* @see layer
33+
*/
3234
QgsMapLayer* currentLayer() const;
3335

36+
/** Return the layer currently shown at the specified index within the combo box.
37+
* @param layerIndex position of layer to return
38+
* @note added in QGIS 2.10
39+
* @see currentLayer
40+
*/
41+
QgsMapLayer* layer( int layerIndex ) const;
42+
3443
public slots:
3544
//! setLayer set the current layer selected in the combo
3645
void setLayer( QgsMapLayer* layer );

src/gui/qgsmaplayercombobox.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ QgsMapLayerComboBox::QgsMapLayerComboBox( QWidget *parent ) :
2424
setModel( mProxyModel );
2525

2626
connect( this, SIGNAL( activated( int ) ), this, SLOT( indexChanged( int ) ) );
27+
connect( mProxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
28+
connect( mProxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
2729
}
2830

2931
void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer )
@@ -45,9 +47,12 @@ void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer )
4547

4648
QgsMapLayer* QgsMapLayerComboBox::currentLayer() const
4749
{
48-
int i = currentIndex();
50+
return layer( currentIndex() );
51+
}
4952

50-
const QModelIndex proxyIndex = mProxyModel->index( i, 0 );
53+
QgsMapLayer *QgsMapLayerComboBox::layer( int layerIndex ) const
54+
{
55+
const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 );
5156
if ( !proxyIndex.isValid() )
5257
{
5358
return 0;
@@ -74,3 +79,16 @@ void QgsMapLayerComboBox::indexChanged( int i )
7479
emit layerChanged( layer );
7580
}
7681

82+
void QgsMapLayerComboBox::rowsChanged()
83+
{
84+
if ( count() == 1 )
85+
{
86+
//currently selected layer item has changed
87+
emit layerChanged( currentLayer() );
88+
}
89+
else if ( count() == 0 )
90+
{
91+
emit layerChanged( 0 );
92+
}
93+
}
94+

src/gui/qgsmaplayercombobox.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,18 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
5252
//! returns the list of excepted layers
5353
QList<QgsMapLayer*> exceptedLayerList() const {return mProxyModel->exceptedLayerList();}
5454

55-
//! currentLayer returns the current layer selected in the combo box
55+
/** Returns the current layer selected in the combo box.
56+
* @see layer
57+
*/
5658
QgsMapLayer* currentLayer() const;
5759

60+
/** Return the layer currently shown at the specified index within the combo box.
61+
* @param layerIndex position of layer to return
62+
* @note added in QGIS 2.10
63+
* @see currentLayer
64+
*/
65+
QgsMapLayer* layer( int layerIndex ) const;
66+
5867
public slots:
5968
//! setLayer set the current layer selected in the combo
6069
void setLayer( QgsMapLayer* layer );
@@ -65,6 +74,7 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
6574

6675
protected slots:
6776
void indexChanged( int i );
77+
void rowsChanged();
6878

6979
private:
7080
QgsMapLayerProxyModel* mProxyModel;

0 commit comments

Comments
 (0)