2

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
    self.dockwidget.features_list.clear()
    for i in range (0,len(features)):
        self.dockwidget.features_list.addItem(str(i))

    layer.selectionChanged.connect(self.showFeatures)


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

    #Zoom
    box = feature.geometry().boundingBox()
    self.iface.mapCanvas().setExtent(box)
    self.iface.mapCanvas().refresh()

    #Anzeigen der Feature Attribute
    self.dockwidget.attributes_list.clear()
    for attribute in feature.attributes():
        self.dockwidget.attributes_list.addItem(str(attribute))

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

2
  • 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
    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) Jul 19, 2018 at 9:58

1 Answer 1

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):
    attributes_list.clear()
    fi = (int(item.text()))
    print(fi)
    feat = layer.selectedFeatures()[fi]
    for attr in feat.attributes():
        attributes_list.addItem(str(attr))

def showFeatures(selected):
    if selected:
        for i in range (0,len(layer.selectedFeatures())):
            features_list.addItem(str(i))
    else:
        features_list.clear()

# set up layer
canvas = iface.mapCanvases()[0]
layer = canvas.currentLayer()
layer.selectionChanged.connect(showFeatures)
features_list = QListWidget()
features_list.itemClicked.connect(showAttributes)
attributes_list = QListWidget()
features_list.show()
attributes_list.show()

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.

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

Your Answer

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

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