0

I am trying to select a feature from the layer using my plugin. When working with my plugin in main Python function using QGIS, I receive an AttributeError when I call a class method from within the class itself.
I'm using QGIS 2.8.

Here is my code below:

def handle_mouse_down(point):
    """"Manage selection when clicking on the canvas."""

    layer = iface.activeLayer()
    canvas = iface.mapCanvas()
    layers = canvas.layers()
    clickTool = QgsMapToolEmitPoint(canvas)

    vl_to_query = [l for l in layers if l.name() != u'my_layer_name'][0]
    w = canvas.mapUnitsPerPixel() * 3
    rect = QgsRectangle(
        point.x() - w,
        point.y() - w,
        point.x() + w,
        point.y() + w
    )

    l_rect = canvas.mapSettings().mapToLayerCoordinates(vl_to_query, rect)
    vl_to_query.select(l_rect, False)
    feats = vl_to_query.selectedFeatures()
    vl_to_query.removeSelection()
    clickTool.canvasClicked.disconnect(self.handle_mouse_down)

When I run this function am getting an error like

File "C:/Users/.qgis2/python/plugins\Myclass\Myclass.py", line 220, in handle_mouse_down point.x() - w,
AttributeError: Myclass instance has no attribute 'x'

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Virat ABD
  • 171
  • 1
  • 13
  • Can you include the actual error message in your question, it will probably contain more pertinent information. – Michael Stimson Aug 07 '18 at 06:15
  • AttributeError: Mycalss instance has no attribute 'x' This is the Actual error that am facing everytime – Virat ABD Aug 07 '18 at 06:22
  • I updated my question please check it – Virat ABD Aug 07 '18 at 06:29
  • 2
    You probably set point = Myclass() somewhere in your code and Myclass has no attribute x. It's hard to tell what problem is just by looking at this part of your Myclass.py file. How do you call handle_mouse_down() method and pass the argument. Did you define x attribute for Myclass class or inherit from any class which has attribute x? – Kadir Şahbaz Aug 07 '18 at 06:45
  • I agree with @Kadir, the problem is most likely you're calling handle_mouse_down with a Myclass object instead of a QgsPoint object... that's one of the dangers of python, in C (# or ++) etc. you'd be unable to compile because the wrong object is being passed. There is an awesome answer here that should help with your mousedown event https://gis.stackexchange.com/questions/45094/programatically-check-for-mouse-click-in-pyqgis and here https://gis.stackexchange.com/questions/108044/getting-coordinates-by-clicking-on-qgis-canvas-with-pyqgis (2nd answer) – Michael Stimson Aug 07 '18 at 22:50

0 Answers0