I am new to QGIS plugin scripting (3.16 Hannover) (in Python 3.7).
I have lots of different layers in my layers panel and I want to be able to click on a button on my GUI, which then activates a function, so I can click on any vector on my map canvas, and then receive the M-value of the chosen feature in the vector layer. I want to store this M-value in a variable, so I can use it later.
First off I tried reading the documentation on https://qgis.org/pyqgis/3.2/gui/Map/QgsMapToolIdentify.html#qgis.gui.QgsMapToolIdentify.identifyVectorLayer But I'm also new to programming in Python, so it's a bit difficult for me to understand.
I got the following code :
def canvas_clicked(self): self.canvas = self.iface.mapCanvas() # make mapcanvas variable self.pointTool = QgsMapToolEmitPoint(self.canvas) # using QgsMapToolEmitPoint to emit point, so i can retrieve x, y of mouse click self.pointTool.canvasClicked.connect(self.store_m_value) # connect function to mouseclick on canvas (in this function I want the program to store m-value of clicked vector in a variable) self.canvas.setMapTool(self.pointTool) # Set the map tool, so i can click the canvas
Then I connect my button (I use Qt designer - where i named my button
btn_canvas_clicked) to the
def run(self) self.dialog.btn_canvas_clicked.connect(self.canvas_clicked)
I tried the following to store the m-value unsuccesfully
def store_m_value(self, point, event) self.identify_vector = QgsMapToolIdentify(self.canvas) self.result = self.identify_vector.identify(point.x(), point.y(), , QgsMapToolIdentify.DefaultQgsSetting) m_value = self.result.geometry().constGet().mAt(0)
I tried using following answer as a guideline for retrieving M value from vector layer: enter link description here