Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyqtSignal and QObject.receivers(..)

I need to check the signal for the presence of the listener, before it is emitted.

class Test(QObject):
    test = pyqtSignal(str,dict)
    def run(self):
        if self.receivers(SIGNAL("test(str,dict)"):
           self.test.emit('blablabla',{})`

The signal is connected to the slot right and successfully emits signals.
When checking the signature signal, the method QObject.receivers() shows that this signal is not connected.
I understood, reason was incorrect signature, I did not find a method, to specify the faithful signature of signal.

like image 624
DrEvil35 Avatar asked Sep 30 '11 10:09

DrEvil35


2 Answers

In pyqt5 SIGNAL is deprecated. It is replaced with signal attribute of each QObject

if QObject.receivers(QObject.signal) > 0:

    print('signal connected')

To Check QPushButton signal clicked() is connected to any slot

button = QPushButton()
.
.
if button.receivers(button.clicked) > 0:
    .....
like image 53
Sudhakar Naidu Avatar answered Oct 14 '22 04:10

Sudhakar Naidu


The signature for your signal is "test(QString, PyQt_PyObject)".

So obviously, str is mapped to QString and other native python object types, dict, list... are mapped to the C++ type PyQt_PyObject.

The list of signal signatures can be obtained through the QMetaObject associated with your object:

test = Test()
metaobject = test.metaObject()
for i in range(metaobject.methodCount()):
    print(metaobject.method(i).signature())
like image 33
alexisdm Avatar answered Oct 14 '22 04:10

alexisdm