0

I try to develop my plugin. The problem is that when the layer is selected in upper comboBox (QgsMapsLayerComboBox), in lower two boxes (Latitude, Longitude: usual ComboBoxes) shall appear fields correspoding with selected layers.

enter image description here

The problem is, that when I switch the layer in upper field, I can only choose the fields which are from the other layer...

enter image description here

On this picture, the fields "ID", "LONGITUDE", "LATITUDE" belongs to previously selected layer "coordinates" visible on 1st photo.

Here's my code of def_run of my plugin:

def run(self):

    self.dlg.layerCombo2.clear()
    self.dlg.layerCombo3.clear()

    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
        layer_list.append(layer.name())
    self.dlg.layerCombo1.addItems(layer_list)
    self.dlg.layerCombo1.setFilters(QgsMapLayerProxyModel.PointLayer)

    def field_select():
        self.dlg.layerCombo2.clear()
        self.dlg.layerCombo3.clear()
        selectedLayerIndex = self.dlg.layerCombo1.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = [field.name() for field in selectedLayer.pendingFields()]
        self.dlg.layerCombo2.addItems(fields)
        self.dlg.layerCombo3.addItems(fields)

    # This connects the function to the layer combobox when changed
    self.dlg.layerCombo1.currentIndexChanged.connect(field_select)
    self.dlg.show()

`

7
  • I cannot see QgsFieldComboBox objects implemented and QgsMapLayerComboBox is used in a wrong way (as a QComboBox).
    – xunilk
    Jan 14, 2018 at 16:22
  • QgsMapLayerComboBox objects automatically select all layers in your Map Canvas. You only need to have one QgsFieldComboBox object, with its respective connecting function, for when you select whatever layer automatically also select its respective fields.
    – xunilk
    Jan 14, 2018 at 16:36
  • Thanks for reply. But I need to have two comboBoxes where I can choose 'X' and 'Y' column from point layer. How to implement QgsMapLayerComboBox and use it?
    – muszynov
    Jan 14, 2018 at 16:38
  • So what shall I do to make a field from which I have to choose a layer and then select fields from selected layer?
    – muszynov
    Jan 14, 2018 at 16:49
  • You can see that in my answer. If you need more Combo Objects add them with different name.
    – xunilk
    Jan 14, 2018 at 19:03

1 Answer 1

2

This is the code snippet in my Test Plugin:

.
.
.
from qgis.gui import QgsMapLayerComboBox, QgsMapLayerProxyModel, QgsFieldComboBox
.
.
.

    def add_action(
.
.
.
        self.wcbL = QgsMapLayerComboBox(self.dlg)
        self.wcbL.move(250, 80)
        self.wcbL.setFixedWidth(203)
        self.wcbL.setFilters(QgsMapLayerProxyModel.PointLayer)

        self.wcbF = QgsFieldComboBox(self.dlg)
        self.wcbF.move(250, 120)
        self.wcbF.setFixedWidth(203)

        return action
.
.
.
    def select_layer_fields(self, vlayer):
        self.wcbF.setLayer(vlayer)
        field = self.wcbF.setLayer(vlayer)

    def run(self):
        """Run method that performs all the real work"""
        vlayer = self.wcbL.currentLayer()
        self.wcbF.setLayer(vlayer)
        self.wcbL.layerChanged.connect(self.select_layer_fields)

        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.
            pass

I tried it out with two point layers; as it can be observed at following images.

With layer points_test (only one field):

enter image description here

When random_points layer was selected in QgsMapLayerComboBox, fields in this layer were automatically also selected in QgsFieldComboBox object (following image):

enter image description here

Plugin works as expected.

Editing Note 1:

Following image also contains a QgsMapLayerComboBox object added directly with QtDesigner. Its name is mMapLayerComboBox.

enter image description here

Editing Note 2:

When Combo Boxes are adding by using QtDesigner following code works as expected:

.
.
.
    def change_layers2(self):                                           #new function
        self.dlg.mMapLayerComboBox.clear()
        layers = self.iface.legendInterface().layers()
        layer_list = [ layer.name() for layer in layers ]
        self.dlg.mMapLayerComboBox.addItems(layer_list)

    def select_layer_fields2(self, vlayer):                              #new function
        self.dlg.mFieldComboBox.setLayer(vlayer)
        field = self.dlg.mFieldComboBox.setLayer(vlayer)

    def run(self):
        """Run method that performs all the real work"""
        vlayer = self.wcbL.currentLayer()
        self.wcbF.setLayer(vlayer)
        self.wcbL.layerChanged.connect(self.select_layer_fields)

        vlayer = self.dlg.mMapLayerComboBox.currentLayer()                           #new line
        self.dlg.mFieldComboBox.setLayer(vlayer)                                     #new line
        self.dlg.mMapLayerComboBox.layerChanged.connect(self.select_layer_fields2)   #new line
.
.
.

It can be observed at following image:

enter image description here

18
  • Thanks. But it is not working. I get the error: TypeError: add_action() got an unexpected keyword argument 'text'
    – muszynov
    Jan 14, 2018 at 19:16
  • Also this error appears when I type def add_action(): " TypeError: add_action() takes no arguments (5 given)"
    – muszynov
    Jan 14, 2018 at 19:19
  • Instead add_action, put combo objects in _init_ method.
    – xunilk
    Jan 14, 2018 at 19:21
  • Well, another error. in init self.wcbL = QgsMapLayerComboBox(self.dlg) AttributeError: DMS2Point instance has no attribute 'dlg'
    – muszynov
    Jan 14, 2018 at 19:23
  • I build my plugins with 'Plugin Builder'. And you?
    – xunilk
    Jan 14, 2018 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.