7

I am trying to get the QgsMapCanvas coordinates when they change using PyQt5 and PyQGIS 3. What I have used for that:

self.canvas.scaleChanged.connect(lambda: self.showScale(self.canvas.scale()))
self.canvas.xyCoordinates.connect(lambda: self.showXY(self.canvas.xyCoordinates()))

and functions definitions are

def showXY(self, p):
    # SLOT. Show coordinates 
    self.lblXY.setText(str(p.x()) + " | " + str(p.y()))

def showScale(self, scale):
    # SLOT. Show scale 
    self.lblScale.setText("Scale 1:" + str(scale))

I am able to get the scale but when the xyCoordiantes is connected to the slot, window goes unresponsive and force closes. What am I doing wrong?

1 Answer 1

5

You don't need to define lambda function and to specify self.canvas.scale() and self.canvas.xyCoordinates(). Because scaleChanged event passes scale and xyCoordinates passes coordinates to the related functions you define, by default. Thus, try in that way:

self.canvas.scaleChanged.connect(self.showScale)
self.canvas.xyCoordinates.connect(self.showXY)

Edit: You sometimes need to pass additional parameters to the method. In this case, use a lambda function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.