2

I'm using QlistView to show my names of departement in the studied area after using the 'extract by location' processus:

# res=QgsVectorLayer()
    # capture  département inside the studied area :
    res = processing.runalg('qgis:extractbylocation', departementLayer, layerZone, u'intersects', 0, None)
    # Créer une couche vector issue de l'extraction des départements dans la zone
 departementLayer = QgsVectorLayer(res['OUTPUT'], "Departement(s) dans la zone d'etude","ogr")      
    features = ObjectifVilleExtract.departementLayer.getFeatures()
    model3 = QStandardItemModel()
    for feature in features:
        item = QStandardItem(feature['name_dept'])
        item.setCheckable(True)
        .model3.appendRow(item)
        self.dlg.listView.setModel(.model3)        

Now I would like to show the code for each departement . I know that I can't use "listview" but I have to use 'Qtableview'. How can I do that?

1

If you want to show only two columns, you can make use of the setHorizontalHeaderItem() method for your model and then fill each row with values for both columns. The following is an example which could be run from the Python Console but you could adapt it for your plugin:

from PyQt4.QtGui import QStandardItem, QStandardItemModel, QTableView

layer = iface.activeLayer()
features = layer.getFeatures()
model3 = QStandardItemModel()
model3.setHorizontalHeaderItem(0, QStandardItem('Department'))
model3.setHorizontalHeaderItem(1, QStandardItem('Code'))
for feature in features:
    item = QStandardItem(str(feature['name_dept'])), QStandardItem(str(feature['code']))
    model3.appendRow(item)

view = QTableView()
view.setModel(model3)
view.show()
  • is there any possibility to set items chekables ? – zak Aug 21 at 7:51
  • @zak - Yes, add item[0].setCheckable(True) after the line item = QStandardItem(...) to make the first column checkable. – Joseph Aug 21 at 9:39
  • 1
    it works perfectly, thanks a lot – zak Aug 22 at 7:57
  • @zak - Most welcome, glad it helped :) – Joseph Aug 22 at 9:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.