Class: QgsFieldComboBox

The QgsFieldComboBox is a combo box which displays the list of fields of a given layer. It might be combined with a QgsMapLayerComboBox to automatically update fields according to a chosen layer. If expression must be used, QgsFieldExpressionWidget shall be used instead.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsFieldComboBox

Base classes

QComboBox

QWidget

QObject

QPaintDevice

Methods

allowEmptyFieldName

Returns True if the combo box allows the empty field ("not set") choice.

currentField

Returns the currently selected field

fields

Returns the fields currently shown in the combobox.

filters

currently used filter on list of fields

indexChanged

layer

Returns the layer currently associated with the combobox.

setAllowEmptyFieldName

Sets whether an optional empty field ("not set") option is shown in the combo box.

setField

setField sets the currently selected field

setFields

Manually sets the fields to use for the combo box.

setFilters

setFilters allows filtering according to the type of field

setLayer

Sets the layer for which fields are listed in the combobox.

Signals

fieldChanged

Emitted when the currently selected field changes.

class qgis.gui.QgsFieldComboBox[source]

Bases: QComboBox

__init__(parent: QWidget | None = None)

QgsFieldComboBox creates a combo box to display the fields of a layer. The layer can be either manually given or dynamically set by connecting the signal QgsMapLayerComboBox.layerChanged to the slot setLayer.

Parameters:

parent (Optional[QWidget] = None)

allowEmptyFieldName(self) bool[source]

Returns True if the combo box allows the empty field (“not set”) choice.

Return type:

bool

currentField(self) str[source]

Returns the currently selected field

Return type:

str

signal fieldChanged(fieldName: str)[source]

Emitted when the currently selected field changes.

Parameters:

fieldName (str)

fields(self) QgsFields[source]

Returns the fields currently shown in the combobox.

This will either be fields from the associated layer() or the fields manually set by a call to setFields().

Added in version 3.14.

Return type:

QgsFields

filters(self) QgsFieldProxyModel.Filters[source]

currently used filter on list of fields

Return type:

QgsFieldProxyModel.Filters

indexChanged(self, i: int)[source]
Parameters:

i (int)

layer(self) QgsVectorLayer | None[source]

Returns the layer currently associated with the combobox.

See also

setLayer()

Return type:

Optional[QgsVectorLayer]

setAllowEmptyFieldName(self, allowEmpty: bool)[source]

Sets whether an optional empty field (“not set”) option is shown in the combo box.

Parameters:

allowEmpty (bool)

setField(self, fieldName: str | None)[source]

setField sets the currently selected field

Parameters:

fieldName (Optional[str])

setFields(self, fields: QgsFields)[source]

Manually sets the fields to use for the combo box.

This method should only be used when the combo box ISN’T associated with a layer() and needs to show the fields from an arbitrary field collection instead. Calling setFields() will automatically clear any existing layer().

See also

fields()

Added in version 3.14.

Parameters:

fields (QgsFields)

setFilters(self, filters: QgsFieldProxyModel.Filters | QgsFieldProxyModel.Filter)[source]

setFilters allows filtering according to the type of field

Parameters:

filters (Union[QgsFieldProxyModel.Filters, QgsFieldProxyModel.Filter])

setLayer(self, layer: QgsMapLayer | None)[source]

Sets the layer for which fields are listed in the combobox. If no layer is set or a non-vector layer is set then the combobox will be empty.

See also

layer()

Parameters:

layer (Optional[QgsMapLayer])