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.
Best Answer
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:
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.