简体   繁体   中英

How to use signal between different modules

I know how to use signal in a module or class like this:

 class A (QObject):
    def __init__(self):
        QObject.__init__(self)

    def afunc (self, i):
        self.emit(SIGNAL("doSomePrinting(int)"), i)    

    def bfunc(self, i):
        print "Hello World!", i
        sys.exit()

if __name__=="__main__":
    app=QCoreApplication(sys.argv)
    a=A()
    QObject.connect(a,SIGNAL("doSomePrinting(int)"),a.bfunc)
    a.afunc(10)    
    sys.exit(app.exec_())

But I don't know how to use signal between different modules, for example: a.py and b.py .I define and bound signal in a class of a.py ,and I want emit the signal to call the bound slot which define in a.py in b.py.

I use this way:

self.logsignal.connect(self.handle) signalMange.signal = self.logsignal

I use a class to store the instance in a.py and I call it in b.py like this:

signalMange.signal.emit('start write project...')

What about my way? Are there better ways?

"a.py"
The class A has a signal called my_signal and we have defined a method_for_signal in our class.

from PyQt4 import QtCore, QtGui

class A(QObject):
    my_signal = QtCore.pyqtSignal(str)

    def __init__():
        super(A, self).__init__()
        self.name = "ABCDEFGHIJKLM"

    def method_for_signal(self):    
        self.my_signal.emit(self.name)

"b.py"

Here the class A is imported in class B, where we want to our signal.

from a import A
class B(QObject):

    def __init__():
        super(B, self).__init__()

    @staticmethod
    def update(name):
        print str(name)

    def signal_connect(self):
        self.obj = A()
        self.obj.my_signal.connect(B.update)

The signal_connect method of class B when invoked will make our signal give the desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM