I have a layer that I can edit with a mouse click on features in the map (dialog will open). Attribute fields can be edited via dropdown menu.

How can I display the currently saved value of a feature in the dropdown menu?

I made the Dialog with Qt Designer.

    def canvasPressEvent(self, event):

        found_features = self.identify(event.x(), event.y(), [self.layer], QgsMapToolIdentify.TopDownAll)
        self.layer.selectByIds([f.mFeature.id() for f in found_features], QgsVectorLayer.AddToSelection)
        mylayer = self.layer

        #get field index
        loadcategory= mylayer.fields().indexFromName('loadcategory')
        seg = mylayer.getSelectedFeatures()

        for j in seg:
            geom = j.geometry()
            # show value in fields

Here I stuck in following line:


            # show dialog frame
            result = self.dlg.exec_()   
            # See if "save" was pressed
            if result:
                # save process:
                self.dlg.loadcategory.itemData(j.setAttribute(loadcategory, self.dlg.loadcategory.currentText()))


How I can I replace .currentText() like for QLineEdit with .setText(j.attributes()[loadcategory])?

I think it is simple but I didn't found the right method in the documentation for QComboBox class yet.


1 Answer 1


As commented by @Benw:

If your combo box is editable, you can just use setCurrentText() method. To also add it to the drop down list, use addItem() method first.

Your Answer

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

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