I've write this QgsMaptool above and it works great in python console
imports...
class obsCreator(QgsMapToolEmitPoint):
def __init__(self, iface):
self.iface = iface
self.canvas = QgsMapCanvas()
QgsMapTool.__init__(self, self.canvas)
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.layer = self.canvas.currentLayer()
self.x = float
self.y = float
def canvasPressEvent(self, event):
point = self.toLayerCoordinates(self.canvas.mouseLastXY())
self.x = point[0]
self.y = point[1]
def canvasReleaseEvent(self, event):
feats = []
pr = self.layer.dataProvider()
fields = self.layer.fields()
fet = QgsFeature
geom = QgsGeometry().fromPointXY(QgsPointXY(self.x, self.y))
fet.setgeometry(geom)
feats.append(fet)
fet.setAttributes([1, 'test'])
if event.button() == Qt.RightButton:
self.canvas.unsetMapTool(self)
else:
pr.addFeatures(feats)
self.layer.triggerRepaint()
pr.addFeatures(feats)
self.layer.triggerRepaint()
but, I want to connect it with a pushButton in my dockwidget:
def run(self):
if not self.pluginIsActive:
self.pluginIsActive = True
if self.dockwidget == None:
self.dockwidget = revEnvDockWidget()
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
# show the dockwidget
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget)
self.dockwidget.show()
# Clear comboBox (useful so we don't create duplicate items in list)
self.dockwidget.obsComboBox.clear()
# Add all items in list to comboBox
self.dockwidget.obsComboBox.addItems(Handler.obsListCombo(self))
canvas_clicked = obsCreator(iface.mapCanvas())
self.dockwidget.hookPushButton.clicked.connect(iface.mapCanvas().setMapTool(canvas_clicked))
and I get this error:
WARNING Traceback (most recent call last):
File "E:\_Code\QGIS\rev_env\rev_env.py", line 268, in run
self.dockwidget.hookPushButton.clicked.connect(iface.mapCanvas().setMapTool(canvas_clicked))
TypeError: argument 1 has unexpected type 'NoneType'
I`ve based my code in this question: How to get co ordinates of points on mouse click in PyQGIS. Am I missing somethig?
self.canvas_clicked = obsCreator(self.iface.mapCanvas())
self.dockwidget.hookPushButton.clicked.connect(lambda: self.iface.mapCanvas().setMapTool(self.canvas_clicked))
Does that work?