2

我有一个要求,我的执行将在线程中发生,我想从该线程向主类发出信号。下面是我的示例代码片段,我在其中创建了一个实例MainClass并将其分配给temp. 其中MainClass内部连接到Thread1将发生实际工作的线程。当信号从我发出时,Thread1我希望signalcalled执行连接的定义temp

代码片段

class Thread1(QtCore.QThread):

    ThreadSignal = QtCore.pyqtSignal(str)

    While True:

        #some statments
        if Condition:
            ThreadSignal.emit('Yes')
        else:
            ThreadSignal.emit('No')

class MainClass(QtCore.QObject):

    MainSignal = QtCore.pyqtSignal(str)

    Testinstance = Thread1()

def signalcalled(s):

    print s

if __name__=='__main__':
    app = AtGui.QApplication(sys.argv)
    temp = MainClass()
    temp.MainSignal.connect(signalcalled)
    sys.exit(app.exec()_)
4

2 回答 2

1

这应该有效:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui

class myThread(QtCore.QThread):
    threadSignal = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(myThread, self).__init__(parent)

    def run(self):
        while True:
            if True:
                self.threadSignal.emit('Yes')

            else:
                self.threadSignal.emit('No')

            break

class myMainClass(QtCore.QObject):
    mainSignal = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(myMainClass, self).__init__(parent)

        testInstance = myThread(self)
        testInstance.threadSignal.connect(self.mainSignal.emit)
        testInstance.start()

def signalcalled(s):
    print s

if __name__ == "__main__":
    import  sys

    app = QtGui.QApplication(sys.argv)
    main = myMainClass()
    main.mainSignal.connect(signalcalled)
    sys.exit(app.exec_())
于 2012-12-16T05:47:28.830 回答
0
class Thread1(QtCore.QThread):

    ThreadSignal = QtCore.pyqtSignal(str)

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

    def run(self):

        While True:

            #some statments
            if Condition:
                ThreadSignal.emit('Yes')
            else:
                ThreadSignal.emit('No')

class MainClass(QtCore.QObject):

    MainSignal = QtCore.pyqtSignal(str)

    def __init__(self):

        super(myThread, self).__init__()

        Testinstance = Thread1()
        Testinstance.ThreadSignal.connect(MainSignal)
        Testinstance.start()

def signalcalled(s):

    print s

if __name__=='__main__':
    app = AtGui.QApplication(sys.argv)
    temp = MainClass()
    temp.MainSignal.connect(signalcalled)
    sys.exit(app.exec()_)
于 2012-12-17T05:48:31.997 回答