0

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.

How can I disconnect the same or is there any other workaround for this problem?

My Code and Dialog box are attached for reference.

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? Commented Jul 3, 2020 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. Commented Jul 4, 2020 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. Commented Jul 4, 2020 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(...). Commented Jul 4, 2020 at 13:20

2 Answers 2

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.

0

I had a similar problem and it's probably caused by calling the first_method() function.

Instead of:

layer.editingStarted.connect(lambda:first_method())
layer.editingStopped.connect(lambda:first_method())

Try:

layer.editingStarted.connect(first_method)
layer.editingStopped.connect(first_method)

It may help, but you'll have to check, because I tried it on my similar example and I'm not sure if it will work for you.

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.