7

I try to build a QGIS plugin to add data into a SpatiaLite database. All relevant tables are load into the QGIS Layers Panel. At first, I want to select a feature in table_1. This table has geometries and could also select in map canvas. I would specify this in a QgsFeatureListComboBox. My approach is

self.dlg.mFeatureListComboBox_losp.setSourceLayer(table_1)

With this, I get the combo box and can choose a feature. But how to select the feature by the combobox and work with this feature and the information?

3
  • 1
    If your aim is to choose a feature using a combobox, QgsFeaturePickerWidget is more useful. You can get selected feature easily by it. Do you have any specific reason for using QgsFeatureListComboBox? Aug 31, 2021 at 7:47
  • Yes my aim is to choose a feature, receive data and copy the data of one field by this feature into an other table in the database. There I want to collect some ofer data. The copy of the data from one field is to have the relationship between the tables. I will try the QgsFeaturePickerWidgetbut how to receive the relevant informations and put it in the other tables?
    – Torsten
    Aug 31, 2021 at 9:11
  • I made some effort but couldn't find a solution using QgsFeatureListComboBox. Therefore, I will add a solution using QgsFeaturePickerWidget. Aug 31, 2021 at 10:39

1 Answer 1

8

If you use QgsFeaturePickerWidget instead of QgsFeatureListComboBox, you can get the selected feature in the combobox easily.

Sample script for the plugin:

def run(self):

    ...
    ...

    layer = self.iface.activeLayer()

    picker = self.dlg.mFeaturePickerWidget
    picker.setLayer(layer)
    picker.setDisplayExpression('$id') #show ids in combobox

    def select_feature():
        layer = self.iface.activeLayer()
        layer.removeSelection()
        
        feature = picker.feature()

        # Do whatever you need with feature

        # For example, select the feature
        layer.select(feature.id())


    button = self.dlg.pushButton
    button.clicked.connect(select_feature)

    # show the dialog
    self.dlg.show()

    ...
    ...

Using the sample script, after you choose a feature id in combobox, you can get all attributes and geometry of the feature using picker.feature().

enter image description here

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.