2

I'm developing a QGIS plugin using QtDesigner and pyqt4. I have a comboBox of my vector layers and I want to show the table of the selected layer. I used this code for my comboBox:

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

    allLayers = self.iface.legendInterface().layers()
    allLyrNames = [lyr.name() for lyr in allLayers]
    self.dlg.updateCombos(allLyrNames)

    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # Do something useful here - delete the line containing pass and
        # substitute with your code.

        grdLayerName = self.dlg.getGridLayer()

        pass

And these two functions:

def getGridLayer(self):
    return(unicode(self.GridLayerCombo.currentText()))
def updateCombos(self, items):
    if len(items) > 0:
        self.GridLayerCombo.clear()

        for item in items:
            self.GridLayerCombo.addItem(item)

1 Answer 1

-1

You can take a look at the bottom part sample from https://webgeodatavore.github.io/pyqgis-samples/gui-group/QgsAttributeTableView.html for a solution (disclosure: I'm the author of the content)

You may also look at https://webgeodatavore.github.io/pyqgis-samples/gui-group/QgsMapLayerComboBox.html for a component to select layers

Both samples are standalone: run them in the QGIS Python console after opening any QGIS project. You will need to change iface reference while integrating in a plugin.

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.