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'

  • Can you include the actual error message in your question, it will probably contain more pertinent information. – Michael Stimson Aug 7 '18 at 6:15
  • AttributeError: Mycalss instance has no attribute 'x' This is the Actual error that am facing everytime – Virat ABD Aug 7 '18 at 6:22
  • I updated my question please check it – Virat ABD Aug 7 '18 at 6: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 7 '18 at 6: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 gis.stackexchange.com/questions/45094/… and here gis.stackexchange.com/questions/108044/… (2nd answer) – Michael Stimson Aug 7 '18 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.