Skip to content
Permalink
Browse files

Fix missing signals, duplicate signals sent by QgsFieldComboBox

  • Loading branch information
nyalldawson committed Jan 4, 2020
1 parent f8e5861 commit f961449cc08991d4bb0ebac2774090dc9d0f6a23
Showing with 35 additions and 4 deletions.
  1. +12 −3 src/gui/qgsfieldcombobox.cpp
  2. +23 −1 tests/src/python/test_qgsfieldcombobox.py
@@ -56,18 +56,27 @@ QgsVectorLayer *QgsFieldComboBox::layer() const

void QgsFieldComboBox::setField( const QString &fieldName )
{
const QString prevField = currentField();
QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
if ( idx.isValid() )
{
QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
if ( proxyIdx.isValid() )
{
setCurrentIndex( proxyIdx.row() );
emit fieldChanged( currentField() );
return;
}
else
{
setCurrentIndex( -1 );
}
}
setCurrentIndex( -1 );
else
{
setCurrentIndex( -1 );
}

if ( prevField != currentField() )
emit fieldChanged( currentField() );
}

QString QgsFieldComboBox::currentField() const
@@ -15,9 +15,9 @@
from qgis.core import QgsFields, QgsVectorLayer, QgsFieldProxyModel
from qgis.gui import QgsFieldComboBox
from qgis.PyQt.QtCore import QVariant, Qt
from qgis.PyQt.QtTest import QSignalSpy

from qgis.testing import start_app, unittest

start_app()


@@ -58,6 +58,28 @@ def testFilter(self):
w.setField('fldint')
self.assertEqual(w.currentField(), 'fldint')

def testSignals(self):
l = create_layer()
w = QgsFieldComboBox()
w.setLayer(l)

spy = QSignalSpy(w.fieldChanged)
w.setField('fldint2')
self.assertEqual(len(spy), 1)
self.assertEqual(spy[-1][0], 'fldint2')
w.setField('fldint2')
self.assertEqual(len(spy), 1)
self.assertEqual(spy[-1][0], 'fldint2')
w.setField('fldint')
self.assertEqual(len(spy), 2)
self.assertEqual(spy[-1][0], 'fldint')
w.setField(None)
self.assertEqual(len(spy), 3)
self.assertEqual(spy[-1][0], None)
w.setField(None)
self.assertEqual(len(spy), 3)
self.assertEqual(spy[-1][0], None)


if __name__ == '__main__':
unittest.main()

0 comments on commit f961449

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