Skip to main content
Added comments to code
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()

    # Define layer listGet andall addloaded layers toin comboBoxesthe interface
    layers = self.iface.legendInterface().layers()
    # Create an empty list which we can populate
    layer_list = []
    # For every item (which we call "layer") in all loaded layers
    for layer in layers:
        # Add it to the list
        layer_list.append(layer.name())
    # Clear comboBox (useful so we don't create duplicate items in list)
    self.dlg.comboBox.clear()
    # Add all items in list to comboBox
    self.dlg.comboBox.addItems(layer_list)
    # Clear comboBox_2
    self.dlg.comboBox_2.clear()
    # Add all items in list to comboBox_2
    self.dlg.comboBox_2.addItems(layer_list)

    def layer_field():
        # Identify selected layer and add fieldsby toits comboBoxesindex
        selectedLayerIndex = self.dlg.comboBox.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        # Identify fields of the selected layer
        fields = selectedLayer.pendingFields()
        # Get field names of the fields
        fieldnames = [field.name() for field in fields]
        # Clear comboBox_5
        self.dlg.comboBox_5.clear()
        # Add field names to comboBox_5
        self.dlg.comboBox_5.addItems(fieldnames)

    def table_field():
        # Identify selected layer and addSame fieldscomments toas comboBoxesabove
        selectedLayerIndex = self.dlg.comboBox_2.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = selectedLayer.pendingFields()
        fieldnames = [field.name() for field in fields]
        self.dlg.comboBox_3.clear()            
        self.dlg.comboBox_3.addItems(fieldnames)
        self.dlg.comboBox_4.clear()
        self.dlg.comboBox_4.addItems(fieldnames) 

    # When changing layer in comboBox, run the function "layer_field()"
    # to refresh the field names in the associated comboBoxes
    self.dlg.comboBox.currentIndexChanged.connect(layer_field)
    self.dlg.comboBox_2.currentIndexChanged.connect(table_field)
def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()

    # Define layer list and add layers to comboBoxes
    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
        layer_list.append(layer.name())
    self.dlg.comboBox.clear()
    self.dlg.comboBox.addItems(layer_list)
    self.dlg.comboBox_2.clear()
    self.dlg.comboBox_2.addItems(layer_list)

    def layer_field():
        # Identify selected layer and add fields to comboBoxes
        selectedLayerIndex = self.dlg.comboBox.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = selectedLayer.pendingFields()
        fieldnames = [field.name() for field in fields]
        self.dlg.comboBox_5.clear()
        self.dlg.comboBox_5.addItems(fieldnames)

    def table_field():
        # Identify selected layer and add fields to comboBoxes
        selectedLayerIndex = self.dlg.comboBox_2.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = selectedLayer.pendingFields()
        fieldnames = [field.name() for field in fields]
        self.dlg.comboBox_3.clear()            
        self.dlg.comboBox_3.addItems(fieldnames)
        self.dlg.comboBox_4.clear()
        self.dlg.comboBox_4.addItems(fieldnames)

    self.dlg.comboBox.currentIndexChanged.connect(layer_field)
    self.dlg.comboBox_2.currentIndexChanged.connect(table_field)
def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()

    # Get all loaded layers in the interface
    layers = self.iface.legendInterface().layers()
    # Create an empty list which we can populate
    layer_list = []
    # For every item (which we call "layer") in all loaded layers
    for layer in layers:
        # Add it to the list
        layer_list.append(layer.name())
    # Clear comboBox (useful so we don't create duplicate items in list)
    self.dlg.comboBox.clear()
    # Add all items in list to comboBox
    self.dlg.comboBox.addItems(layer_list)
    # Clear comboBox_2
    self.dlg.comboBox_2.clear()
    # Add all items in list to comboBox_2
    self.dlg.comboBox_2.addItems(layer_list)

    def layer_field():
        # Identify selected layer by its index
        selectedLayerIndex = self.dlg.comboBox.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        # Identify fields of the selected layer
        fields = selectedLayer.pendingFields()
        # Get field names of the fields
        fieldnames = [field.name() for field in fields]
        # Clear comboBox_5
        self.dlg.comboBox_5.clear()
        # Add field names to comboBox_5
        self.dlg.comboBox_5.addItems(fieldnames)

    def table_field():
        # Same comments as above
        selectedLayerIndex = self.dlg.comboBox_2.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = selectedLayer.pendingFields()
        fieldnames = [field.name() for field in fields]
        self.dlg.comboBox_3.clear()            
        self.dlg.comboBox_3.addItems(fieldnames)
        self.dlg.comboBox_4.clear()
        self.dlg.comboBox_4.addItems(fieldnames) 

    # When changing layer in comboBox, run the function "layer_field()"
    # to refresh the field names in the associated comboBoxes
    self.dlg.comboBox.currentIndexChanged.connect(layer_field)
    self.dlg.comboBox_2.currentIndexChanged.connect(table_field)
Source Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286

You can try replacing your def run(self): function in your script for the one below which should do something you want:

def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()

    # Define layer list and add layers to comboBoxes
    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
        layer_list.append(layer.name())
    self.dlg.comboBox.clear()
    self.dlg.comboBox.addItems(layer_list)
    self.dlg.comboBox_2.clear()
    self.dlg.comboBox_2.addItems(layer_list)

    def layer_field():
        # Identify selected layer and add fields to comboBoxes
        selectedLayerIndex = self.dlg.comboBox.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = selectedLayer.pendingFields()
        fieldnames = [field.name() for field in fields]
        self.dlg.comboBox_5.clear()
        self.dlg.comboBox_5.addItems(fieldnames)

    def table_field():
        # Identify selected layer and add fields to comboBoxes
        selectedLayerIndex = self.dlg.comboBox_2.currentIndex()
        selectedLayer = layers[selectedLayerIndex]
        fields = selectedLayer.pendingFields()
        fieldnames = [field.name() for field in fields]
        self.dlg.comboBox_3.clear()            
        self.dlg.comboBox_3.addItems(fieldnames)
        self.dlg.comboBox_4.clear()
        self.dlg.comboBox_4.addItems(fieldnames)

    self.dlg.comboBox.currentIndexChanged.connect(layer_field)
    self.dlg.comboBox_2.currentIndexChanged.connect(table_field)