PyQGIS Documentation - Linfiniti Geo Blog
PyQGIS Documentation - Linfiniti Geo Blog
PyQGIS Documentation - Linfiniti Geo Blog
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>PyQGIS</strong> <strong>Documentation</strong>, Release 1.4<br />
def keyActionF7(self):<br />
QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed F7")<br />
12.5.2 How to toggle Layers (work around)<br />
Note: from QGIS 1.5 there is QgsLegendInterface class that allows some manipulation with list of layers within<br />
legend.<br />
As there is currently no method to directly access the layers in the legend, here is a workaround how to toggle the<br />
layers using layer transparency:<br />
def toggleLayer(self, lyrNr):<br />
lyr = self.iface.mapCanvas().layer(lyrNr)<br />
if lyr:<br />
cTran = lyr.getTransparency()<br />
lyr.setTransparency(0 if cTran > 100 else 255)<br />
self.iface.mapCanvas().refresh()<br />
The method requires the layer number (0 being the top most) and can be called by:<br />
self.toggleLayer(3)<br />
12.5.3 How to access attribute table of selected features<br />
def changeValue(self, value):<br />
layer = self.iface.activeLayer()<br />
if(layer):<br />
nF = layer.selectedFeatureCount()<br />
if (nF > 0):<br />
layer.startEditing()<br />
ob = layer.selectedFeaturesIds()<br />
b = QVariant(value)<br />
if (nF > 1):<br />
for i in ob:<br />
layer.changeAttributeValue(int(i),1,b) # 1 being the second column<br />
else:<br />
layer.changeAttributeValue(int(ob[0]),1,b) # 1 being the second column<br />
layer.commitChanges()<br />
else:<br />
QMessageBox.critical(self.iface.mainWindow(),"Error", "Please select at least one feature from<br />
else:<br />
QMessageBox.critical(self.iface.mainWindow(),"Error","Please select a layer")<br />
The method requires the one parameter (the new value for the attribute field of the selected feature(s)) and can be<br />
called by:<br />
self.changeValue(50)<br />
12.5.4 How to debug a plugin using PDB<br />
First add this code in the spot where you would like to debug:<br />
12.5. Code Snippets 41