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):
        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")

        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):
        #selecting the feature or features that intersect with mouse click buffer

#code continues to guide user data entry using python logic
  • You are mixing a lot of concepts in a single class there. Is this the mouse click that opens the form or a button on the form that that does something more? – Nathan W Mar 22 '14 at 3:09
  • i just updated the post to explain a bit more. also forgot to mention that there are no extra buttons on my form that do more. – user25976 Mar 23 '14 at 0:23

Browse other questions tagged or ask your own question.