1

On QgsProject.readProject I add a slot to some layer's selectionChanged. I tried disconnecting these slots in QgsProject.layersWillBeRemoved but here the project seems already closed, so I can't retrieve the layers to disconnect the slots.

There is a secont signature layersWillBeRemoved(self, layers: Iterable[QgsMapLayer]) but I failed to connect to it.

@pyqtSlot(list)
def layersWillBeRemovedHandler(self, layers):
  #disconnect

... 

project.layersWillBeRemoved.connect(self.layersWillBeRemovedList) #without [list]

throws decorated slot has no signature compatible with layersWillBeRemoved(QStringList).

project.layersWillBeRemoved[list].connect(self.layersWillBeRemovedHandler)

throws there is no matching overloaded signal whether the handler is decorated or not with @pygtSlot(list).

So how do I solve this? Do I need to disconnect them at all, when the project is closed (or the layer is removed from the project)?

  • 1
    Is there any reason why you'd like to disconnect it yourself? When a QObject object (like QgsProject or QgsMapLayer) is destroyed, signals to and from the object are disconnected for you. See stackoverflow.com/questions/9264750/… and Qt docs – Germán Carrillo Mar 26 at 11:57
  • Oh, great. I read a tutorial somewhere (can't find it anymore), that said, always disconnect you should!. So that was obviously wrong. – gumo Mar 26 at 12:59
  • Then I'll post the comment as an answer, I think it's worth marking it as accepted so that other users searching for this topic can solve their doubt. – Germán Carrillo Mar 26 at 13:09
1

Is there any reason why you'd like to disconnect it yourself?

When a QObject object (like QgsProject or QgsMapLayer) is destroyed, signals to and from the object are disconnected for you. See Qt docs and this thread in SO.

Therefore, as soon as the layer is removed or the project is closed, the layer object will be deleted, therefore its SIGNAL-SLOT connections (including selectionChanged) will be automatically disconnected.

| improve this answer | |

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.