I am making a plugin in QGIS which involves the use of QgsFieldComboBox, what I want to do is that the combo box only shows fields with data type of string (or to show fields with data type of integer, etc). Does anyone have any code example?
comboBox.setLayer(layer)
comboBox.setFilters(???)
Also, setFilters() takes QgsFieldProxyModel filters as parameter but when I do:
from qgis.core import QgsFieldProxyModel
It gives me an error saying that ImportError: cannot import name QgsFieldProxyModel
Best Answer
I assume that you are fairly experienced making plugins and you know where put different code lines in __init__, initGui, etc functions of your plugin. So, next code works at Python Console and you should adapt it for your plugin structure:
I tried it out with a point and polygon layers of next images. They have only one field with data type of string in its attributes tables. Observe that filter QgsFieldProxyModel.String allows, in each case, that the QgsFieldComboBox object only shows one field with data type of string (as expected). As line commentary, you have all other possibilities for filters (Double, Int, etc).
When point layer is selected:
When polygon layer is selected: