3

I'm a little bit confused (I'm a newbie) on how to get a list of just the selected Items of a QListWidget (in a QGIS plugin).

I set up the manageGui function in this way:

def manageGui(self):

    selectedLayers = []

    # Trying to set up the listWidgetView
        self.listWidget.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked|QtGui.QAbstractItemView.EditKeyPressed)
        self.listWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        self.listWidget.setViewMode(QtGui.QListView.ListMode)

        # Load the layers in the listWidget
        layers = []
        for i in QgsMapLayerRegistry.instance().mapLayers().values():
            #load only POLYGON layers, to filer out useless layers
            if i.geometryType() == QGis.Polygon:
                self.layers.append(i.name())

        # Populate the listWidget with all the polygon layer present in the TOC
        self.listWidget.addItems(self.layers)

        #put the selected layers in a list
        selectedLayers = self.listWidget.selectedItems()

but the listWidget.selectedItems() seems empty. If I print selectedLayers I just get [].

I also tried to run a loop over the listWidget:

        #popultate the list with only the selected layers of the list
        for item in self.listWidget.selectedItems():
            self.selectedLayers.append(item)

but it doesn't work neither, always and empty list.

1
  • It's easier to do that with an object of the QgsMapLayerComboBox class (see my answer).
    – xunilk
    Commented Dec 28, 2015 at 10:30

2 Answers 2

2

It seems to me, that you want to fill the list before you even seleceted any items. First you fill it and in the next step you already want the selected items.

You would have to register an event and specify a function what to do at this specific moment. For example when a button is pressed or an item is selected, then you get the selected layers.

Something like:

def get_selected_layers(self):
  selectedLayers = self.listWidget.selectedItems()
  # do the rest of your code

You will find the Qt Signals for the QListWidget in the docs and the easiest way to register the event is in the Qt Designer's Signals and Slots View (F4), where you can connect the Signal to the designated function.

7
  • thanks for the answer.. I'm pretty new to all the Qt stuff.. So my question is: I have to connect the OK button (that performs all the operations) with the signal of the QListWidget?
    – matteo
    Commented Dec 1, 2015 at 13:25
  • As I understand it, you would bind a click() Signal to the OK-Button with a Bind to the function when_ok_pressed() in which you can get the self.listWidget.selectedItems()
    – bennos
    Commented Dec 1, 2015 at 13:35
  • I just wanted to have a list of the selected items names of the listWidget. This way I can pass the list of the items to another function
    – matteo
    Commented Dec 1, 2015 at 13:50
  • I am not sure if you are asking me a question. In my answer I described a way how to get the selected items of the listWidget. At this point you can get the names of the items and pass the list to another function.
    – bennos
    Commented Dec 1, 2015 at 13:58
  • Ok.. I just don't understand if have I to add the connection between the listWidget and the button in Qt Designer too..
    – matteo
    Commented Dec 1, 2015 at 14:08
1

It's easier to do that with an object of the QgsMapLayerComboBox class because the list of layers is automatically incorporated from the detected environment. You can set filters from QgsMapLayerProxyModel class to load only polygon layers. It is so easy that you can use the Python Console to try out the approach.

In my example, I loaded three vector layers (two polygons and one point layer type) in QGIS; as it can be observed at the next image:

enter image description here

I used the next code:

from qgis.gui import *
wcb = QgsMapLayerComboBox()
wcb.setFilters(QgsMapLayerProxyModel.PolygonLayer)
wcb.show()

to get this result:

enter image description here

You can observe that the filter works as it expected. There are only polygon layers at the Combo Box.

In the plugin, the Combo Box is instancied at the __init__ method. The slot function or run method is used to get the layer reference with the 'currentLayer' method of QgsMapLayerComboBox. The following snipped code it is a summary of the procedure:

.
.
.
    def __init__(self, iface):
.
.
.         
        self.wcbL = QgsMapLayerComboBox(self.dlg)
        self.wcbL.move(150,28)
        self.wcbL.setMinimumWidth(203)
        self.wcbL.setFilters(QgsMapLayerProxyModel.PolygonLayer)
.
.
.
    def run(self):
        """Run method that performs all the real work"""
        vlayer = self.wcbL.currentLayer()
.
.
.
        # show the dialog
        self.dlg.show()
.
.
.

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.