[Qgis-user] Check if a selectionChanged signal is connected to any slot?

Matthias Kuhn matthias at opengis.ch
Tue Oct 3 05:09:54 PDT 2017


Hi Mariane,

I don't see the usage of Qt.UniqueConnection in your example.

This works for me:


def executeSel():
	print('selection changed')

layer.selectionChanged.connect(executeSel, Qt.UniqueConnection)
layer.selectionChanged.connect(executeSel, Qt.UniqueConnection)
Traceback (most recent call last):
  File "/usr/lib64/python3.5/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: connection is not unique

try:
    layer.selectionChanged.connect(executeSel, Qt.UniqueConnection)
except TypeError:
    pass # No reason to complain if it was already connected



Matthias

On 10/03/2017 01:48 PM, Tudorache, Marian wrote:
> Hi Mathias,
> 
>  
> 
> Yeah, this is what I knew in theory. Unfortunately this is not the case.
> 
> You can try in python console.
> 
> Make sure you have a layer with some features.
> 
> Define function executeAfterSelection where you do some dummy printing.
> 
> Call three times layer.selectionChanged.connect(executeAfterSelection).
> 
> Then try to select features in the layer.
> 
> You will see the function executeAfterSelection is execute three times
> after each selection.
> 
>  
> 
> I have done an workaround by introducing a flag variable which tells me
> if the myLayer.selectionChanged.connect(executeAfterSelection) statement
> was already called.
> 
>  
> 
> Thanks,
> 
> Marian
> 
>  
> 
> *From:*Qgis-user [mailto:qgis-user-bounces at lists.osgeo.org] *On Behalf
> Of *Matthias Kuhn
> *Sent:* October-03-17 3:13 AM
> *To:* qgis-user at lists.osgeo.org
> *Subject:* Re: [Qgis-user] Check

 if a selectionChanged signal is
> connected to any slot?
> 
>  
> 
> Hi Marian,
> 
> Just tell Qt that it shouldn't create the connection more than once
> using Qt::UniqueConnection.
> 
> Cheers
> 
> Matthias
> 
>  
> 
> On 10/2/17 5:30 PM, Tudorache, Marian wrote:
> 
>      
> 
>     Hi everyone,
> 
>      
> 
>     I have connected the signal selectionChanged so some code, which is
>     executed after features are selected.
> 
>      
> 
>     myLayer.selectionChanged.connect(self.executeAfterSelection)
> 
>      
> 
>     Then I have a method self. executeAfterSelection(self, featuresIds)
> 
>      
> 
>     The SIGNAl/SLOT tandem works but now I realized every time when I
>     call *myLayer.selectionChanged.connect(self.executeAfterSelection)*
>      It does not overwrite my previous call. It creates a second
>     signa/slot tandem. During running the method  executeAfterSelection
>     is excuted twice, three times etc.
> 
>      
> 
>     How can I check if the SIGNAL selectionChanged is connected to the
>     slot executeAfterSelection before I run
>     myLayer.selectionChanged.connect(self.executeAfterSelection)?
> 
>      
> 
>     I tried this:
> 
>     receiversCount =
>     QtCore.QObject.receivers(QtCore.SIGNAL(“selectionChanged()”)
> 
>     receiversCount =
>     QtCore.QObject.receivers(QtCore.SIGNAL(“selectionChanged”)
> 
>     receiversCount =
>     QtCore.QObject.receivers(QtCore.SIGNAL(“myLayer.selectionChanged()”)
> 
>     receiversCount =
>     QtCore.QObject.receivers(QtCore.SIGNAL(“myLayer.selectionChanged”)
> 
>      
> 
>     Every time I get this error:
> 
>      
> 
>     QObject.receivers(SIGNAL()): first argument of unbound method must
>     have type ‘QObject’).
> 
>      
> 
>      
> 
>     Thanks,
> 
>     Marian
> 
>      
> 
>      
> 
>      
> 
>      
> 
>     ------------------------------------------------------------------------
> 
> 
>     This electronic message, as well as any transmitted files included
>     in the electronic message, may contain privileged or confidential
>     information and is intended solely for the use of the individual(s)
>     or entity to which it is addressed. If you have received this
>     electronic message in error please notify the sender immediately and
>     delete the electronic message. Any unauthorized copying, disclosure
>     or distribution of the electronic message is strictly forbidden. NAV
>     CANADA accepts no liability for any damage caused by any virus
>     and/or other malicious code transmitted by this electronic
>     communication.
> 
>     Le présent message électronique et tout fichier qui peut y être
>     joint peuvent contenir des renseignements privilégiés ou
>     confidentiels destinés à l’usage exclusif des personnes ou des
>     organismes à qui ils s’adressent. Si vous avez reçu ce message
>     électronique par erreur, veuillez en informer l’expéditeur
>     immédiatement et supprimez le. Toute reproduction, divulgation ou
>     distribution du présent message électronique est strictement
>     interdite. NAV CANADA n’assume aucune responsabilité en cas de
>     dommage causé par tout virus ou autre programme malveillant transmis
>     par ce message électronique.
> 
> 
> 
>     _______________________________________________
> 
>     Qgis-user mailing list
> 
>     Qgis-user at lists.osgeo.org <mailto:Qgis-user at lists.osgeo.org>
> 
>     List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> 
>     Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> 
>  
> 



More information about the Qgis-user mailing list