2

I am creating a plugin in which i need to get coordinates by clicking on QGIS canvas so i tried the solution mentionned at Getting coordinates by clicking on QGIS Canvas with PyQGIS? but it didn't work for me , but this one Programatically check for mouse click in PyQGIS? seems working well now i want to pass the coordinates to GUI LineEdit, using QGIS 2.8.6, Python 2.7 and PyQT4 but it's not working so here's my code :


from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis import *
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication,QObject
from PyQt4.QtGui import QAction, QIcon

import os.path
from qgis.core import QgsApplication,QgsMapLayerRegistry
from qgis.gui import QgsMapCanvas,QgsMapToolEmitPoint,QgsMapTool

# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from Proejt_dialog import ProjetDialog
from Proejt_dialog import ProjetDialog as ProjetDialog1
import os.path
class Projet:
   def __init__(self, iface):
        """Constructor.

        :param iface: An interface instance that will be passed to this class
            which provides the hook by which you can manipulate the QGIS
            application at run time.
        :type iface: QgisInterface
        """
        # Save reference to the QGIS interface
        self.h=0
        self.iface = iface

        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        # initialize locale
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            'i18n',
            'Projet_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)


        # Declare instance attributes
        self.actions = []
        self.menu = self.tr(u'&Projet')
        # TODO: We are going to let the user set this up in a future iteration
        self.toolbar = self.iface.addToolBar(u'Projet')
        self.toolbar.setObjectName(u'Projet')
   def functions(self):
        do stuffs
   def run(self):
        """Run method that performs all the real work"""
        tool = PointTool(self.iface.mapCanvas())
        self.iface.mapCanvas().setMapTool(tool)
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.
            pass
class PointTool(QgsMapTool):   
    def __init__(self, canvas):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas    
        self.ui = ProjetDialog1()

    def canvasPressEvent(self, event):
        pass

    def canvasMoveEvent(self, event):
        x = event.pos().x()
        y = event.pos().y()

        point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
        self.ui.X.setText(str(point[0]))
        self.ui.Y.setText(str(point[1]))
    def canvasReleaseEvent(self, event):
        #Get the click
        x = event.pos().x()
        y = event.pos().y()

        point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)

        P=Projet(self) #error

        P.dlg.X.setText(str(point[0]))
        P.dlg.Y.setText(str(point[1]))

    def activate(self):
        pass

    def deactivate(self):
        pass

    def isZoomTool(self):
        return False

    def isTransient(self):
        return False

    def isEditTool(self):
        return True

any help please, i'm still new to PyQGIS

Update : i couldn't link the class 'PointTool' to the dialog initialised in the 'Projet' class, so i added a parent parameter which contain the dialog to the 'PointTool' then call it in run function from Projet Class

1

so here's my new code

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis import *
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication,QObject
from PyQt4.QtGui import QAction, QIcon

import os.path
from qgis.core import QgsApplication,QgsMapLayerRegistry
from qgis.gui import QgsMapCanvas,QgsMapToolEmitPoint,QgsMapTool

# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from Proejt_dialog import ProjetDialog
from Proejt_dialog import ProjetDialog as ProjetDialog1
import os.path
class Projet:
   def __init__(self, iface):
        """Constructor.

        :param iface: An interface instance that will be passed to this class
            which provides the hook by which you can manipulate the QGIS
            application at run time.
        :type iface: QgisInterface
        """
        # Save reference to the QGIS interface
        self.h=0
        self.iface = iface

        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        # initialize locale
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            'i18n',
            'Projet_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)


        # Declare instance attributes
        self.actions = []
        self.menu = self.tr(u'&Projet')
        # TODO: We are going to let the user set this up in a future iteration
        self.toolbar = self.iface.addToolBar(u'Projet')
        self.toolbar.setObjectName(u'Projet')
   def functions(self):
        do stuffs
   def run(self):
        """Run method that performs all the real work"""
        tool = PointTool(self.iface.mapCanvas(),parent=self) #define this class as parent
        self.iface.mapCanvas().setMapTool(tool)
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.
            pass
class PointTool(QgsMapTool):   
    def __init__(self, canvas,parent=None): #add parent parametre
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas    
        self.ui = ProjetDialog1()
        self.parent = parent

    def canvasPressEvent(self, event):
        pass

    def canvasMoveEvent(self, event):
        x = event.pos().x()
        y = event.pos().y()

        point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)

    def canvasReleaseEvent(self, event):
        #Get the click
        x = event.pos().x()
        y = event.pos().y()

        point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)

        self.parent.dlg.X.setText(str(point[0])) # call parent(Projet) dialog
        self.parent.dlg.Y.setText(str(point[1]))

    def activate(self):
        pass

    def deactivate(self):
        pass

    def isZoomTool(self):
        return False

    def isTransient(self):
        return False

    def isEditTool(self):
        return True
  • 3
    Besides publishing your solution it would be nice also to add some explanation what was wrong before and how you managed to solve it. – TomazicM Jun 8 at 17:43
  • 1
    Please include explanation in the body of your answer, not as comment. – TomazicM Jun 10 at 17:26

Your Answer

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

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