Skip to content
Permalink
Browse files

Fix QgsFieldComboBox::setField sets incorrect field when filter is pr…

…esent

(cherry-picked from 6e49403)
  • Loading branch information
nyalldawson committed Jul 21, 2017
1 parent 250b959 commit 0c335a257c3ebc3b1b9eb6ddb37f69365ee5e4a7
Showing with 67 additions and 1 deletion.
  1. +1 −1 src/gui/qgsfieldcombobox.cpp
  2. +1 −0 tests/src/python/CMakeLists.txt
  3. +65 −0 tests/src/python/test_qgsfieldcombobox.py
@@ -59,7 +59,7 @@ void QgsFieldComboBox::setField( const QString& fieldName )
QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
if ( proxyIdx.isValid() )
{
setCurrentIndex( idx.row() );
setCurrentIndex( proxyIdx.row() );
emit fieldChanged( currentField() );
return;
}
@@ -45,6 +45,7 @@ ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
ADD_PYTHON_TEST(PyQgsExpressionLineEdit test_qgsexpressionlineedit.py)
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
ADD_PYTHON_TEST(PyQgsFieldComboBoxTest test_qgsfieldcombobox.py)
ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py)
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
ADD_PYTHON_TEST(PyQgsField test_qgsfield.py)
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsFieldComboBox
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '20/07/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.core import QgsFields, QgsVectorLayer
from qgis.gui import QgsFieldComboBox, QgsFieldProxyModel
from qgis.PyQt.QtCore import QVariant, Qt

from qgis.testing import start_app, unittest

start_app()


def create_layer():
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer&field=fldint2:integer",
"addfeat", "memory")
assert layer.isValid()
return layer


def create_model():
l = create_layer()
m = QgsFieldModel()
m.setLayer(l)
return l, m


class TestQgsFieldComboBox(unittest.TestCase):

def testGettersSetters(self):
""" test combobox getters/setters """
l = create_layer()
w = QgsFieldComboBox()
w.setLayer(l)
self.assertEqual(w.layer(), l)

w.setField('fldint')
self.assertEqual(w.currentField(), 'fldint')

def testFilter(self):
""" test setting field with filter """
l = create_layer()
w = QgsFieldComboBox()
w.setLayer(l)
w.setFilters(QgsFieldProxyModel.Int)
self.assertEqual(w.layer(), l)

w.setField('fldint')
self.assertEqual(w.currentField(), 'fldint')


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

0 comments on commit 0c335a2

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