I'm creating a custom editing form using QGIS 2.0 and would like to modify the mouse event when creating a point. I've gotten a start by reading Programatically check for mouse click in PyQGIS?
In fact, the code below is copied and modified directly from it.
My goal: When creating a new point, I would like to retreive feature attributes of another (line) layer that intersects with the mouse click, using a rectangle filter. The mouse click opens the form when creating the point, while automatically retrieving data from another line layer intersecting with the rectangle buffer. This is in the interest of comparing data in these two layers that are supposed to correspond.
Following the lead of the mentioned Q&A, i'm attempting to make use of the following code without creating a separate Map Canvas. To put it to use, what would I provide, if anything, as the Python Init function under Layer Properties > Fields? Currently I have mypyfile.formOpen as I've added python logic as detailed here: http://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/
I may be going about this in the wrong way, so i'm open to suggestions to direct me towards creating this data collection tool.
class PointTool(QgsMapTool): def __init__(self, canvas): QgsMapTool.__init__(self, canvas) self.canvas = self.iface.mapCanvas() self.iface = iface self.pointTool = QgsMapToolEmitPoint(self.canvas) def run(self): self.canvas.setMapTool(self.pointTool) QObject.connect(self.pointTool, SIGNAL("canvasClicked(const QgsPoint, Qt::MouseButton)", self.selectFeature)) def selectFeature(self, point): #resLayer previously defined if not resLayer or resLayer.type() != QgsMapLayer.VectorLayer(): QMessageBox.warning(None, "No!", "Select a vectorlayer") return width = self.iface.mapCanvas().mapUnitsPerPixel()*2 rect = QgsRectangle(point.x() - width, point.y() - width, point.x() + width, point.y() + width) rect = self.iface.mapCanvas().mapRenderer().mapToLayerCoordinates(layer, rect) resLayer.select(, rect) feat = QgsFeature() ids =  while resLayer.nextFeature(feat): ids.append(feat.id()) #selecting the feature or features that intersect with mouse click buffer resLayer.setSelectedFeatures(ids) #code continues to guide user data entry using python logic