I adapted the following code from a QGIS 2.18 plugin to a QGIS 3.2 plugin:

    def showFeatures(self):
    layer = self.iface.activeLayer()
    features = layer.selectedFeatures()

    #Anzeigen der ausgewählten Features
    for i in range (0,len(features)):


def showAttributes(self):
    if not self.dockwidget.features_list.selectedIndexes():
    item = self.dockwidget.features_list.selectedItems()[0]
    feature = self.selectedFeatures[int(item.text())]

    box = feature.geometry().boundingBox()

    #Anzeigen der Feature Attribute
    for attribute in feature.attributes():

The plugin I adapted the code from writes the code in the file plugin_dockwidget.py, I write it in the plugin.py. The plugin refers to two list widgets. The first function shows the currently selected features in the first list widget. What the second function should do, is to show a list of attributes of the feature I selected in the first list widget and to zoom on the feature.

My problem is, that the plugin shows the features as expected, but it neither shows the attributes nor does it zoom onto the selected feature.

features_list displays the feautres attributes_list should display the attributes

  • I just saw, that in QT Designer theres a signal for features_list defined, that emits if the current item is changed and connects to the slot update(). But it does still not work if I also create the signal.
    – gHupf
    Commented Jul 19, 2018 at 7:46
  • I assume that showFeatures is not registering itself? see line layer.selectionChanged.connect(self.showFeatures)! Then in showFeatures you have a second parameter, which holds the id's of the selectedFeatures. With your code, i can't see, that showAttributes is ever executed? Try to connect everyting when the plugin is started (if you used PluginBuilder, look into the run() method, there is an if statement, if self.dockwidget == None, inside that if-clause is a good point to set up signals and slots) Commented Jul 19, 2018 at 9:58

1 Answer 1


I broke it down to a simple example. Here, at least one vector layer must be loaded in QGIS and it must be the top one. Then you can execute this in the python console as a script:

from PyQt5.QtWidgets import QListWidget
def showAttributes(item):
    fi = (int(item.text()))
    feat = layer.selectedFeatures()[fi]
    for attr in feat.attributes():

def showFeatures(selected):
    if selected:
        for i in range (0,len(layer.selectedFeatures())):

# set up layer
canvas = iface.mapCanvases()[0]
layer = canvas.currentLayer()
features_list = QListWidget()
attributes_list = QListWidget()

If you want to rerun the script, make sure you delete the layer and reload it, otherwise it will connect double to showFeatures.

Inside your plugin you should connect signals-slots for layer and features_list at a point where your plugin is "created", with PluginBuilder you'll find the if clause inside the run() method. You should make sure, it's only connected once, even if your plugin is going to be closed and reopend by the user.

  • Thanks for the answer! I will test it in a few days, since I do not have time right now.
    – gHupf
    Commented Jul 25, 2018 at 10:47

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.