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

Alexandre Neto senhor.neto at gmail.com
Thu Aug 22 08:50:45 PDT 2013


It worked well!

Actually, looking into Bernhard code I have found the same solution (the
try block).

Thank you, Nathan, Denis and Bernhard for all your help.

Cheers,

Alexandre Neto


On Thu, Aug 22, 2013 at 4:36 PM, Nathan Woodrow <madmanwoo at gmail.com> wrote:

> Hey Alexandre,
>
> Just a refectoring mistake. I noticed that selectionChanged passes a layer
> then forgot that you are connecting other signals which don't.
>
> You get that type error if the self.toggle is not connected to any
> signals, you can just wrap it in a try block.
> On 23/08/2013 1:29 AM, "Alexandre Neto" <senhor.neto at gmail.com> wrote:
>
>> Hello Nathan,
>>
>> 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).
>>
>> 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?
>>
>> An error has occured while executing Python code:
>>> TypeError: toggle() takes exactly 2 arguments (1 given)
>>> Python version:
>>> 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]
>>> Traceback (most recent call last):
>>>   File
>>> "C:\Users\alexandre.neto/.qgis2/python/plugins\splitmultipart\splitmultipart.py",
>>> line 84, in toggle
>>>     layer.editingStopped.disconnect(self.toggle)
>>> TypeError: 'instancemethod' object is not connected
>>>
>>
>> Also, trying to use,
>>
>> layer.editingStopped.disconnect(self.toggle)
>>
>>
>> instead of,
>>
>> QObject.disconnect(layer,SIGNAL("editingStopped()"),self.toggle)
>>
>>
>> Gives me this other error:
>>
>> An error has occured while executing Python code:
>>> Traceback (most recent call last):
>>>   File
>>> "C:\Users\alexandre.neto/.qgis2/python/plugins\splitmultipart\splitmultipart.py",
>>> line 83, in toggle
>>>     layer.editingStopped.disconnect(self.toggle)
>>> TypeError: 'instancemethod' object is not connected
>>> Python version:
>>> 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]
>>>
>>
>> Thank you very much for your help,
>>
>> Alexandre Neto
>>
>>
>> The new code would be:
>>
>>     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
>>>         self.action.triggered.connect(self.run)
>>>         self.iface.currentLayerChanged.connect(self.toggle)
>>>         self.canvas.selectionChanged.connect(self.toggle)
>>>         # Add toolbar button and menu item
>>>         self.iface.advancedDigitizeToolBar().addAction(self.action)
>>>         self.iface.editMenu().addAction(self.action)
>>>
>>>     def toggle(self):
>>>         layer = self.canvas.currentLayer()
>>>
>>>         # Decide whether the plugin button is enable or disable
>>>         if layer:
>>>             # set enable
>>>             if layer.isEditable() and layer.selectedFeatureCount() > 0:
>>>                 self.action.setEnabled(True)
>>>                 layer.editingStopped.connect(self.toggle)
>>>                 layer.editingStarted.disconnect(self.toggle)
>>>             # set disable
>>>             else:
>>>                 self.action.setEnabled(False)
>>>                 layer.editingStarted.connect(self.toggle)
>>>                 layer.editingStopped.disconnect(self.toggle)
>>>
>>
>>
>>
>> On Thu, Aug 22, 2013 at 1:29 PM, Nathan Woodrow <madmanwoo at gmail.com>wrote:
>>
>>> This is not going to solve you problem yet but this is a quick refactor
>>> to use the new style signal slot syntax:
>>>
>>> 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
>>>         self.action.triggered.connect(self.run)
>>>         self.iface.currentLayerChanged.connect(self.toggle)
>>>
>>>         # Add toolbar button and menu item
>>>         self.iface.advancedDigitizeToolBar().addAction(self.action)
>>>         self.iface.editMenu().addAction(self.action)
>>>
>>>     def toggle(self, layer):
>>>         mc = self.canvas
>>>         layer = mc.currentLayer()
>>>
>>>         # Decide whether the plugin button is enable or disable
>>>         if layer:
>>>             # set enable
>>>             if layer.isEditable():
>>>                 layer.editingStopped.connect(self.toggle)
>>>                 layer.editingStarted.disconnect(self.toggle)
>>>                  layer.selectionChanged.connect(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)
>>>                 layer.editingStarted.connect(self.toggle)
>>>                 layer.editingStopped.disconnect(self.toggle)
>>>                 layer.selectionChanged.disconnect(self.toggle)
>>>                  self.iface.messageBar().pushMessage("Debug","
>>> Disconnect SIGNAL(selectionChanged()",0)
>>>
>>>
>>> On Thu, Aug 22, 2013 at 10:11 PM, Alexandre Neto <senhor.neto at gmail.com>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/7abbf1fb/attachment.html>


More information about the Qgis-developer mailing list