Which version of QGIS are you using? If it's 2.4, you can set the columns shown in a QgsComposerAttributeTable by using the setColumns method. This method takes a list of QgsComposerTableColumn. Here's an example
#create a composition and composer attribute table
composition = QgsComposition(iface.mapCanvas().mapSettings())
table = QgsComposerAttributeTable(composition)
#create columns for the table
col1 = QgsComposerTableColumn()
col1.setAttribute('feature_id')
col2 = QgsComposerTableColumn()
col2.setAttribute('feature_name')
#set table columns
table.setColumns([col1, col2])
There's a bunch of other things you can set for columns, such as the heading, alignment and sort order (see api docs here). You can even set an expression for a column in place of the attribute for added flexibility.
edit:
To show only certain features you can set a filter on the table:
table.setFeatureFilter("id in (1,2,3)")
table.setFilterFeatures( True )
So, to filter a table to only selected features:
#get current layer
current_layer = qgis.utils.iface.mapCanvas().currentLayer()
#get list of selected feature ids
selected_ids = [f.id() for f in current_layer.selectedFeatures()]
#convert list to string
filter_id_string = ','.join([str(id) for id in selected_ids])
#filter table
table.setFeatureFilter("$id in (" + filter_id_string + ")")
table.setFilterFeatures( True )
update:
I missed a character in the filter expression. It should have been "$id in (...". Here's a complete, working version of the code you posted. There was a few simple mistakes (the constructor for QgsLegendModel takes no arguments, there's no applyDefaultSize method for attribute tables, and I've also moved the attribute table setItemPosition line):
from PyQt4.QtCore import *
from PyQt4.QtGui import *
layer = qgis.utils.iface.activeLayer()
qgis.utils.iface.actionZoomToSelected().trigger()
qgis.utils.iface.mapCanvas().zoomScale(1000)
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x,y,w*.75,h)
c.addItem(composerMap)
legend = QgsComposerLegend(c)
c.addComposerLegend(legend)
legend.model().setLayerSet(mapRenderer.layerSet())
legend.setTitle('')
legendModel = QgsLegendModel()
legendModel.setLayerSet([layer.id()])
legend.updateLegend()
selected_ids = [f.id() for f in layer.selectedFeatures()]
filter_id_string = ','.join([str(id) for id in selected_ids])
table = QgsComposerAttributeTable(c)
table.setItemPosition(x+50, y+50)
table.setVectorLayer(layer)
table.setMaximumNumberOfFeatures(20)
table.setFeatureFilter("$id in (" + filter_id_string + ")")
table.setFilterFeatures(True)
col1 = QgsComposerTableColumn()
col1.setAttribute('Diametre')
col1.setHeading("Diametre")
col2 = QgsComposerTableColumn()
col2.setAttribute('Nature')
col2.setHeading("Nature")
table.setColumns([col1, col2])
c.addItem(table)
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName('out.pdf')
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QPrinter.Color)
printer.setResolution(c.printResolution())
pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
c.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()
Best Answer
You can make a query on the vector layer by right-clicking on it
-> Query
, filtering out the objects you don't want to see. By changing or deleting the query filter entry, you can create the other views, or full view again.This does not change the shapefile itself, only the project file.