Here is an example which you can run in an editor in the Python console. This should provide you with an example of adding items to the list widget from a list of field names from the current layer in the map layer combo box and updating the fields whenever the layer is changed; allowing multiple selections with:
self.fld_list.setSelectionMode(QListWidget.ExtendedSelection)
clearing the selection, and retrieving the QgsField
objects from the list widget selected items. My example creates a dictionary of field names and field objects by calling the text()
method on the QListWidgetItem
objects returned by the selectedItems()
method.
class Dlg(QDialog):
def __init__(self):
QDialog.__init__(self)
self.layout = QGridLayout(self)
self.cb_layer = QgsMapLayerComboBox()
self.cb_layer.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.cb_layer.layerChanged.connect(self.layer_changed)
self.fld_list = QListWidget(self)
self.fld_list.setSelectionMode(QListWidget.ExtendedSelection)
self.fld_list.addItems([f.name() for f in self.cb_layer.currentLayer().fields()])
self.btn1 = QPushButton('Clear selection', self)
self.btn1.clicked.connect(lambda: self.fld_list.clearSelection())
self.te1 = QTextEdit(self)
self.btn2 = QPushButton('Retrieve selected fields', self)
self.btn2.clicked.connect(self.get_selection)
self.layout.addWidget(self.cb_layer, 0, 0)
self.layout.addWidget(self.fld_list, 1, 0)
self.layout.addWidget(self.btn1, 2, 0)
self.layout.addWidget(self.te1, 3, 0)
self.layout.addWidget(self.btn2, 4, 0)
def layer_changed(self, layer):
self.fld_list.clear()
flds = [f.name() for f in layer.fields()]
self.fld_list.addItems(flds)
def get_selection(self):
inds = [i.text() for i in self.fld_list.selectedItems()]
flds = {}
for i in inds:
flds[i] = [f for f in self.cb_layer.currentLayer().fields() if f.name() == i][0]
self.te1.setText(str(flds))
W = Dlg()
W.show()
Result: