[Qgis-developer] Need help with Plugin button behavior connected to SIGNAL("selectionChanged()")

Denis Rouzaud denis.rouzaud at gmail.com
Thu Aug 22 05:37:29 PDT 2013


Hi Alexandre,

Why don't you use selectionChanged on mapCanvas?
You have a line commented, I suppose this was it.

Mainly, I suppose the problem is that you never disconnect you layer 
from the signals since you always call the method for the current 
layer....so the previous current is never disconnected....

Hope this helps,

Cheers,

Denis


On 08/22/2013 02:11 PM, Alexandre Neto wrote:
> Hello all,
>
> I'm updating the Multipart split plugin 
> <http://plugins.qgis.org/plugins/splitmultipart/> to make it work with 
> the 2.0 API.
>
> 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.
>
> 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()").
>
> Can someone please, point me in the right direction, or tell me where 
> can I find some code example doing this?
>
> Thank you very much,
>
> Alexandre Neto
>
> Part of the code bellow:
>
> def initGui(self):
>         # Create action that will start plugin configuration
>         self.action = QAction(
>             QIcon(":/plugins/splitmultipart/icon.svg"),
>             QCoreApplication.translate('Multipart split', u"Split 
> Selected Multipart features"), self.iface.mainWindow())
>         self.action.setEnabled(False)
>         # connect to signals for button behavior
>         QObject.connect(self.action, SIGNAL("triggered()"), self.run)
>         QObject.connect(self.iface, 
> SIGNAL("currentLayerChanged(QgsMapLayer*)"), self.toggle)
>         #QObject.connect(self.iface, 
> SIGNAL("selectionChanged(QgsMapLayer *)"), self.toggle)
>
>         # Add toolbar button and menu item
> self.iface.advancedDigitizeToolBar().addAction(self.action)
>         self.iface.editMenu().addAction(self.action)
>     def toggle(self):
>         mc = self.canvas
>         layer = mc.currentLayer()
>         # Decide whether the plugin button is enable or disable
>         if layer <> None:
>             # set enable
>             if layer.isEditable():
> QObject.connect(layer,SIGNAL("editingStopped()"),self.toggle)
>                 QObject.connect(layer, SIGNAL("selectionChanged()"), 
> self.toggle)
> QObject.disconnect(layer,SIGNAL("editingStarted()"),self.toggle)
> self.iface.messageBar().pushMessage("Debug"," Connect 
> SIGNAL(selectionChanged()",0)
>                 if layer.selectedFeatureCount() > 0:
>                     self.action.setEnabled(True)
>                 else:
>                     self.action.setEnabled(False)
>             # set disable
>             else:
>                 self.action.setEnabled(False)
> QObject.connect(layer,SIGNAL("editingStarted()"),self.toggle)
> QObject.disconnect(layer,SIGNAL("editingStopped()"),self.toggle)
>                 QObject.disconnect(layer, 
> SIGNAL("selectionChanged()"), self.toggle)
> self.iface.messageBar().pushMessage("Debug"," Disconnect 
> SIGNAL(selectionChanged()",0)
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130822/216076b1/attachment-0001.html>


More information about the Qgis-developer mailing list