I am developing a simple plugin at the moment which contains 3 combo boxes:

  • comboBox_1
  • comboBox_2
  • comboBox_3

Combobox 1 & 2 lists the vector layers available, however I would like the 3rd combobox to list the attribute column that both layers have in common (from the attribute table).

    # run method that performs all the real work
def run(self):
    # show the dialog
    for layer in self.iface.legendInterface().layers():
        if layer.type() == QgsMapLayer.VectorLayer:
  • This project could be interesting for you: 3nids.github.io/qgiscombomanager – Matthias Kuhn Apr 1 '14 at 11:04
  • The info from the link will be useful to me later on. At the moment, I am focusing on connecting the combobox with the Attribute Fields but I do not know the correct terms to use. – Joseph Apr 1 '14 at 12:26

You could achieve this based on the QGIS Combo Manager

Just create your own filter for the fields in a subclass of FieldCombo. The following code is untested (literally), so please bear with me if there's a typo or other minor problems, and think of it as a reference.

from qgiscombomanager import *

class MyFieldCombo( FieldCombo ):
    def __init__(self, widget, vectorLayerCombo1, vectorLayerCombo2, initField="", options={}):
        FieldCombo.__init__(self, widget, vectorLayerCombo1, vectorLayerCombo2, initField, options)
        self.vectorLayerCombo1 = vectorLayerCombo1
        self.vectorLayerCombo2 = vectorLayerCombo2

    # Overrides the filter from FieldCombo. Will be called for every field
    # and should return True, if the field should be shown.
    def __isFieldValid(self, idx):
        if not FieldCombo.__isFieldValid(self, idx):
            return False

        for f in self.vectorLayerCombo2.pendingFields():
            # adjust this line to reflect your idea of "common"
            if f.name() == self.vectorLayerCombo1.pendingFields()[idx].name():
                return True
        return False

# run method that performs all the real work
def run(self):
    # show the dialog
    self.vlc1 = VectorLayerCombo( self.dlg.comboBox )
    self.vlc2 = VectorLayerCombo( self.dlg.comboBox_2 )
    self.fc = MyFieldCombo( self.dlg.comboBox_3 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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