2

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?

3
  • The clicked.connect() signal/ slot expects a function argument. You can pass a lambda function. Try changing last 2 lines: self.canvas_clicked = obsCreator(self.iface.mapCanvas()) self.dockwidget.hookPushButton.clicked.connect(lambda: self.iface.mapCanvas().setMapTool(self.canvas_clicked)) Does that work?
    – Ben W
    Commented Nov 4, 2019 at 13:34
  • Thanks @BenW, it works perfectly, with you want to answer the question I'll vote for best answer. Commented Nov 5, 2019 at 10:54
  • 1
    Great- glad to hear it works. I will post an answer tomorrow. Otherwise, I don't mind at all if you want to answer your own question based on my comment.
    – Ben W
    Commented Nov 5, 2019 at 11:13

1 Answer 1

2

Based on @BenW comment I use a lambda function to pass a dockwidget parameter and it works perfectly.

 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))

            self.canvas_clicked = HookCreator(self.iface.mapCanvas(), self.dockwidget)
            self.dockwidget.hookPushButton.clicked.connect(lambda: self.iface.mapCanvas().setMapTool(self.canvas_clicked))

Your Answer

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

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