1

I am creating a Plugin, where on addFeature button, first user will get a dialog box to select the type of feature user wants to draw. On the basis of users choice, other editing constraints will get activated as defined for a layer. Currently I am able to create a dialog box with radio buttons for users selection and things are working correctly for first run, if user is following below steps in order.

  1. Start editing (Toggle Editing)
  2. Add Feature button
  3. Dialog box for users choice
  4. Editing by the user as per applied constraints
  5. Stop Editing (Toggle Editing)

The above steps are working fine for first run, but if user starts another editing session, then the dialog box is getting for 2 times and so on for further sessions.

While working on this, I found layer.editingStarted.connect(function), this signal is getting continued even after complete execution of function. I tried to disconnect this but not able to do it. Kindly suggest how I can disconnect the same or any other workaround for this problem.

My Code and Dialog box are attached for reference. Please check and suggest.

lyr=self.iface.activeLayer()
lyr.editingStarted.connect(lambda:fiber_add_feature(db,lyr))
def fiber_add_feature(self,db,vlyr):
    if vlyr.name()=="Fiber Cable":
        iface.actionAddFeature().triggered.connect(lambda: self.fiber_route_type(db,vlyr))
def fiber_route_type(self,db,lyr):
    if lyr.name()=="Fiber Cable":
        self.route_dlg=FiberRouteTypeDialog()
        self.duct_require_dlg=DuctRequirementDialog()
        if lyr.isEditable():
            ar="Aerial"
            ugr="Under Ground Route"
            Duct_presnt=True
            self.route_dlg.show()
            route_result = self.route_dlg.exec_()
            if route_result:
                if self.route_dlg.ar_rb.isChecked():
                    fiber = fiberEditTool(self.canvas, self.iface,db,ar, not(Duct_presnt))
                    self.canvas.setMapTool(fiber)
                elif self.route_dlg.ugr_rb.isChecked():
                    self.duct_require_dlg.show()
                    duct_result = self.duct_require_dlg.exec_()
                    if duct_result:
                        if self.duct_require_dlg.cb_duct.isChecked():
                            fiber = fiberEditTool(self.canvas, self.iface,db,ugr,Duct_presnt)
                            self.canvas.setMapTool(fiber)
                        elif self.duct_require_dlg.cb_wo_duct.isChecked():
                            fiber = fiberEditTool(self.canvas, self.iface,db,ugr,not(Duct_presnt))
                            self.canvas.setMapTool(fiber)

enter image description here

4
  • Couldn't you just execute the iface.actionAddFeature().triggered.connect(...) line in the initGui() of your plugin? Or perhaps just discard the 2nd and 3rd lines of your script above? Jul 3 '20 at 13:02
  • @GermánCarrillo, I tried with iface.actionAddFeature().triggered.connect(...), the problem with this command is, it is getting triggered at all layers not on the particular layer for which editing constraints are defined. This is the reason, i am writing lyr.editingStarted.connect(...), just to call the action on the particular active layer. Jul 4 '20 at 10:28
  • Yes, but you could go for your specific layer using its name: gis.stackexchange.com/questions/136861/… and only set the connection for that specific layer. Jul 4 '20 at 13:08
  • Correct, In the code, I mentioned same condition for layer on layer name (vlyr.name()=="Fiber Cable") but iface.actionAddFeature().triggered.connect(...), it is getting triggered irrespective of layer name if i write it outside the other function without lyr.editingStarted.connect(...). Jul 4 '20 at 13:20
1

While doing some research for above question, I found some details to stop editingStarted signal with following code.

    layer=iface.activeLayer()
    if layer.name()=="Fiber":
        layer.editingStarted.connect(lambda:first_method())
        layer.editingStopped.connect(lambda:first_method())
    def first_method():
        if layer.isEditable():
            print("In first method")
       else:
           try:
               layer.editingStarted.disconnect()
           except:
               print("editingStarted signal is not disconnected")
           try:
               layer.editingStopped.disconnect()
           except:
               print("editingStopped signal is not disconnected")

Things are working fine for single layer, but if I have multiple layers in project and I start and stop editing for other layer and again start editing for my required layer at then Qgis is not starting editing for my required layer. Please suggest the workaround for this.

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.