<p dir="ltr">Hey Alexandre,</p>
<p dir="ltr">Just a refectoring mistake. I noticed that selectionChanged passes a layer then forgot that you are connecting other signals which don't.</p>
<p dir="ltr">You get that type error if the self.toggle is not connected to any signals, you can just wrap it in a try block.</p>
<div class="gmail_quote">On 23/08/2013 1:29 AM, "Alexandre Neto" <<a href="mailto:senhor.neto@gmail.com">senhor.neto@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hello Nathan,<div><br></div><div>After I resolved my problem with Denis suggestion I tried to apply your refactor to use the new style. The code seems much simpler. But I got two errors while running it (see code below).</div>

<div><br></div><div>If I add 'layer' as an argument for the toggle function, I get the following error. It works without the change, can you please explain why should layer be passed as an argument too?</div><div>

<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#000000">An error has occured while executing Python code:</font><font color="#000000"><br>

</font><font color="#000000">TypeError: toggle() takes exactly 2 arguments (1 given)</font><font color="#000000"><br></font><font color="#000000">Python version:<br></font><font color="#000000">2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]</font><font color="#000000"><br>

</font><font color="#000000">Traceback (most recent call last):<br></font><font color="#000000">  File "C:\Users\alexandre.neto/.qgis2/python/plugins\splitmultipart\splitmultipart.py", line 84, in toggle<br></font><font color="#000000">    layer.editingStopped.disconnect(self.toggle)<br>

</font><font color="#000000">TypeError: 'instancemethod' object is not connected</font><br></blockquote><div><br></div><div>Also, trying to use, </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

layer.editingStopped.disconnect(self.toggle)</blockquote><div><br></div><div>instead of,</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

QObject.disconnect(layer,SIGNAL("editingStopped()"),self.toggle)</blockquote><div><br></div><div>Gives me this other error:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<span style="color:rgb(255,0,0)">An error has occured while executing Python code:</span><br>Traceback (most recent call last):<br>  File "C:\Users\alexandre.neto/.qgis2/python/plugins\splitmultipart\splitmultipart.py", line 83, in toggle<br>

    layer.editingStopped.disconnect(self.toggle)<br>TypeError: 'instancemethod' object is not connected<br>Python version:<br>2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]<br></blockquote><div>

<br></div><div>Thank you very much for your help,<br></div><div><br></div><div>Alexandre Neto</div><div><br></div><div><br></div><div>The new code would be:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

    def initGui(self):<br>        # Create action that will start plugin configuration<br>        self.action = QAction(<br>            QIcon(":/plugins/splitmultipart/icon.svg"),<br>            QCoreApplication.translate('Multipart split', u"Split Selected Multipart features"), self.iface.mainWindow())<br>

        self.action.setEnabled(False)<br>        <br>        # connect to signals for button behavior<br>        self.action.triggered.connect(self.run)<br>        self.iface.currentLayerChanged.connect(self.toggle)<br>        self.canvas.selectionChanged.connect(self.toggle)<br>

        # Add toolbar button and menu item<br>        self.iface.advancedDigitizeToolBar().addAction(self.action)<br>        self.iface.editMenu().addAction(self.action)<br>    <br>    def toggle(self):<br>        layer = self.canvas.currentLayer()<br>

        <br>        # Decide whether the plugin button is enable or disable<br>        if layer:<br>            # set enable<br>            if layer.isEditable() and layer.selectedFeatureCount() > 0:<br>                self.action.setEnabled(True)<br>

                layer.editingStopped.connect(self.toggle)<br>                layer.editingStarted.disconnect(self.toggle)<br>            # set disable    <br>            else:<br>                self.action.setEnabled(False)<br>

                layer.editingStarted.connect(self.toggle)<br>                layer.editingStopped.disconnect(self.toggle)<br></blockquote></div><div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Thu, Aug 22, 2013 at 1:29 PM, Nathan Woodrow <span dir="ltr"><<a href="mailto:madmanwoo@gmail.com" target="_blank">madmanwoo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr">This is not going to solve you problem yet but this is a quick refactor to use the new style signal slot syntax:<div><br></div><div><div><div>def initGui(self):</div><div>        # Create action that will start plugin configuration</div>



<div>        self.action = QAction(</div><div>            QIcon(":/plugins/splitmultipart/icon.svg"),</div><div>            QCoreApplication.translate('Multipart split', u"Split Selected Multipart features"), self.iface.mainWindow())</div>



<div>        self.action.setEnabled(False)</div><div>        </div><div>        # connect to signals for button behavior</div></div><div>        self.action.triggered.connect(self.run)</div><div>        self.iface.currentLayerChanged.connect(self.toggle)</div>

<div>

<div><br></div><div>        # Add toolbar button and menu item</div><div>        self.iface.advancedDigitizeToolBar().addAction(self.action)</div><div>        self.iface.editMenu().addAction(self.action)</div><div>    </div>



</div><div>    def toggle(self, layer):</div><div><div>        mc = self.canvas</div><div>        layer = mc.currentLayer()</div><div>        </div><div>        # Decide whether the plugin button is enable or disable</div>

</div><div>        if layer:</div><div>

<div>            # set enable</div><div>            if layer.isEditable():</div></div><div>                layer.editingStopped.connect(self.toggle)</div><div>                layer.editingStarted.disconnect(self.toggle)</div>

<div>

                layer.selectionChanged.connect(self.toggle)</div><div><div><br></div><div>                self.iface.messageBar().pushMessage("Debug"," Connect SIGNAL(selectionChanged()",0)</div>
<div>                if layer.selectedFeatureCount() > 0:</div>

<div>                    self.action.setEnabled(True)</div><div>                else:</div><div>                    self.action.setEnabled(False) </div><div>            # set disable    </div><div>            else:</div>


<div>
                self.action.setEnabled(False)</div></div><div>                layer.editingStarted.connect(self.toggle)</div><div>                layer.editingStopped.disconnect(self.toggle)</div><div>                layer.selectionChanged.disconnect(self.toggle)</div>

<div>

<div>                self.iface.messageBar().pushMessage("Debug"," Disconnect SIGNAL(selectionChanged()",0)</div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>
On Thu, Aug 22, 2013 at 10:11 PM, Alexandre Neto <span dir="ltr"><<a href="mailto:senhor.neto@gmail.com" target="_blank">senhor.neto@gmail.com</a>></span> wrote:<br>

</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div>Hello all,</div><div><br></div><div>I'm updating the <a href="http://plugins.qgis.org/plugins/splitmultipart/" target="_blank">Multipart split plugin</a> to make it work with the 2.0 API.</div>



<div><br></div><div>Has I have successfully updated the plugin to make it work in 2.0, I would like to improve it's button behavior to work like the rest of the advanced editing toolbar. I am able to enable and disable it if the current layer is editable. </div>




<div><br></div><div>Now I'm trying to get the same behavior if there are selected features. I have made it work, but QGIS starts to get really slow... I found out that's because lots of connections are being created to SIGNAL("selectionChanged()").</div>




<div><br></div><div>Can someone please, point me in the right direction, or tell me where can I find some code example doing this?</div><div><br></div><div>Thank you very much,</div><div><br></div><div>Alexandre Neto</div>




<div><br></div><div>Part of the code bellow:</div><div><br></div><div><div>def initGui(self):</div><div>        # Create action that will start plugin configuration</div><div>        self.action = QAction(</div><div>            QIcon(":/plugins/splitmultipart/icon.svg"),</div>




<div>            QCoreApplication.translate('Multipart split', u"Split Selected Multipart features"), self.iface.mainWindow())</div><div>        self.action.setEnabled(False)</div><div>        </div><div>




        # connect to signals for button behavior</div><div>        QObject.connect(self.action, SIGNAL("triggered()"), self.run)</div><div>        QObject.connect(self.iface, SIGNAL("currentLayerChanged(QgsMapLayer*)"), self.toggle)</div>




<div>        #QObject.connect(self.iface, SIGNAL("selectionChanged(QgsMapLayer *)"), self.toggle)</div><div><br></div><div>        # Add toolbar button and menu item</div><div>        self.iface.advancedDigitizeToolBar().addAction(self.action)</div>




<div>        self.iface.editMenu().addAction(self.action)</div><div>    </div><div>    def toggle(self):</div><div>        mc = self.canvas</div><div>        layer = mc.currentLayer()</div><div>        </div><div>        # Decide whether the plugin button is enable or disable</div>




<div>        if layer <> None:</div><div>            # set enable</div><div>            if layer.isEditable():</div><div>                QObject.connect(layer,SIGNAL("editingStopped()"),self.toggle)</div>



<div>
                QObject.connect(layer, SIGNAL("selectionChanged()"), self.toggle)</div><div>                QObject.disconnect(layer,SIGNAL("editingStarted()"),self.toggle)</div><div>                self.iface.messageBar().pushMessage("Debug"," Connect SIGNAL(selectionChanged()",0)</div>




<div>                if layer.selectedFeatureCount() > 0:</div><div>                    self.action.setEnabled(True)</div><div>                else:</div><div>                    self.action.setEnabled(False)</div><div>




                    </div><div>            # set disable    </div><div>            else:</div><div>                self.action.setEnabled(False)</div><div>                QObject.connect(layer,SIGNAL("editingStarted()"),self.toggle)</div>




<div>                QObject.disconnect(layer,SIGNAL("editingStopped()"),self.toggle)</div><div>                QObject.disconnect(layer, SIGNAL("selectionChanged()"), self.toggle)</div><div>                self.iface.messageBar().pushMessage("Debug"," Disconnect SIGNAL(selectionChanged()",0)</div>




</div></div>
<br></div></div>_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org" target="_blank">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</blockquote></div>