1

I am new to scripting, as the following question will indicate, but I am trying to build a QGIS python plugin. The plugin has two comboboxes, the second one should show a list of fields from the layer selected in the first combobox. Here is my script:

def run(self):
    """Run method that performs all the real work"""

    layers = self.iface.legendInterface().layers()
    layer_list = []
    self.dlg.comboBox.clear()
    for layer in layers:
        layer_list.append(layer.name())
        self.dlg.comboBox.addItems(layer_list)
    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    print result


    index=self.dlg.comboBox.currentIndex()
    layer = self.dlg.comboBox.itemData(index)
    Field_list = []
    self.dlg.comboBox_2.clear()
    for field in layer.pendingFields():
        Field_list.append(field.name())
        self.dlg.comboBox_2.addItems(layer_list)
    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    print result

But when i run this i get an error:

Traceback (most recent call last): File "C:/Users/carmel.han/.qgis2/python/plugins\zoomtoselect\zoomto_select.py", line 201, in run layer = self.dlg.comboBox.setCurrentLayer() AttributeError: 'QComboBox' object has no attribute 'setCurrentLayer

I suspect i am missing a module. Anybody knows if that is in fact the problem and how do i fix it? I have 4 modules in my PyQt4 folder: QtCore, QtGui, QtNetwork & QtXml.

  • Problem is because you are confusing a QComboBox with a QgsMapLayerComboBox. – xunilk Feb 25 '18 at 13:05
  • On the other hand, 'setCurrentLayer' neither does it exist in QgsMapLayerComboBox class. Method is 'currentLayer'. – xunilk Feb 25 '18 at 14:13
  • Do you mind explaning the differince between QComboBox vs.QgsMapLayerComboBox? I get the smae error with 'currentLayer': AttributeError: 'QComboBox' object has no attribute 'currentLayer' – Carmel Han Feb 26 '18 at 7:03
  • @CarmelHan - QComboBox stores an a list of texts. It does not understand anything about layers. From your code, you created a list and added this to the combobox where each item is given an index. So the first item has an index of 0, second item has an index of 1 etc. If you want to define your layer, what I normally do is retrieve the item from the combobox by name. So try replacing layer = self.dlg.comboBox.setCurrentLayer() with layer = QgsMapLayerRegistry.instance().mapLayersByName( self.dlg.comboBox.currentText() )[0]). – Joseph Feb 26 '18 at 10:34
  • @Joseph, Thank you for your informitive comment, i tried your sugestion, but it returns a list, not a layer, and therefore I cano't use 'pendingFields' to get the fields. What I'm loking fo is how to retrive the layer based on the list that QComboBox stores. Would love to hear if you have more sugestions – Carmel Han Feb 27 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.