1

Trying to get click coordinates from map canvas but code is not working. Newbie for Python, pyqgis, pyqt

I already check these topics but still code is NOT working

1 - QGIS 3 Get coordinates from mouse click

2 - Programatically check for mouse click in PyQGIS?

3 - How to get co ordinates of points on mouse click in PyQGIS

Here is my code that I am trying

import os

from PyQt4 import QtGui, uic
from qgis.gui import QgsMapToolEmitPoint
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'click_coor_dialog_base.ui'))


class ClickCoorDialog(QtGui.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(ClickCoorDialog, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.showCoor)
    def showCoor(self):
        # this QGIS tool emits as QgsPoint after each click on the map canvas
        pointTool = QgsMapToolEmitPoint(iface.mapCanvas())
        pointTool.canvasClicked.connect(self.display_point)
        iface.mapCanvas().setMapTool( self.pointTool )
    def display_point(self,point):
        print point[0]

No matter what changes i do in above code, the display_point function does NOT get called, when I clicked on qgis map canvas. But it supposed to be called

What mistake I am doing here?

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.