I made a little plugin in QGIS, and I got some strange results.
The program now mirrors the "Layers Panel". When I add or delete layers from the Panel the `QListWidget` module get the names of layers.
I connected the function to the `legendLayersAdded` and to `layersRemoved` SIGNAL.

THE PROBLEM:
The add part is ok, and if I remove ONE layer that's fine too. But if I remove multiple layers, the widget gets back the rest of layers' names multiple times (count of removed layers).

Community edit: For however many multiple layers are removed, the remaining layers appear in the list that many times. Ergo, if 3 layers are removed, and there is 1 remaining, that layer appears 3 times in the additional layer pane:

[![image of the issue][1]][1]

It's like the signal calls the function recursively!?
I tried with the layerWasRemoved Signal and the result was the same
I made a video of it because it is hard to explain. 

[https://www.youtube.com/watch?v=NuLtANtlLtE&feature=youtu.be]

Here is the code:

    def ablak(self):
    layers = [] 
    self.dockwidget.telepules_kezelo.clear()
    layers = self.iface.legendInterface().layers()
    print "Befor" # Befor layer list
    print layers # Layer list "from Layers Panel"
    print "After" # After layer list
    i = 0 # Just a counter for debubing
    lista = self.dockwidget.telepules_kezelo
    for layer in layers:
        print layer.name()
        lista.addItem(layer.name())
        i = i+1
        print i # Counter for debuging
    layers = []

    
    QgsMapLayerRegistry.instance().legendLayersAdded.connect(self.ablak)

    QgsMapLayerRegistry.instance().layersRemoved.connect(self.ablak)


I fade out the problem with transfer the `self.dockwidget.telepules_kezelo.clear()` line after the `Print After` line. And it's working fine, but I am really frustrated with it..

Any help will be welcome.


  [1]: https://i.sstatic.net/vtAFj.png