2

I´m setting up an database for an archaeological project with PSQL. There I have a field "material" connected with a table "materials" containing things like metal, stone, etc. In a third table I have stored more specific data on the materials like "gold", "iron" or "tuff". They all cary an attribute to which material they belong.

Now I set up a custom form in QGIS which works quite well. But now to my problem: I want to choose a material in the first combobox and the specification in a second one. The entries of that field should be filtered by the first one. So when I choose "metal" in the first field the second field should just show me iron, gold and copper.

2

First i would create a list of categories and a dictionary containing the categories and related specific material like

self.cat_list = ['metal', 'stone', 'wood']
self.mat_dict = {'metal':['gold', 'iron', 'copper'],
       'stone':['flint', 'granite', 'wackestone']}

and so on.

Consider you comboBoxes are

self.comboBox_category

and

self.comboBox_mat

populate the first combo with cat_list and connect a method that dependently populates the second:

for c in self.cat_list:
    self.comboBox_category.insertItem(c)

#updateMatlist is the function called when a category is selected
self.comboBox_category.currentIndexChanged.connect(self.updateMatlist)

now implement the method mentioned above

def updateMatlist(self):
    #empty list
    for n in range(self.comboBox_mat.count()):
        self.comboBox_mat.removeItem(n)
    #repopulate combo
    for mat in self.mat_dict[self.comboBox_category.currentText()]:
        self.comboBox_mat.insertItem(mat)

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