PyQt QThreads Communication

时间:2017-04-24 21:40:05

标签: python python-2.7 pyqt pyqt4 python-multithreading

PyQt应用有一个QThread正在运行SenderWorker,每秒都会持续发出信号temperatures。第二个QThread正在运行ReceiverWorker将接收此发出的信号并将其打印到屏幕上。

问题:但是ReceiverWorker没有响应发出的信号。也许使用以下代码行将它们连接在一起不起作用?

self.receiverWorker.connectSlots(self.senderWorker)

以下是整个代码:

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import time



class Screen(QMainWindow):
    def __init__(self):
        super(Screen, self).__init__()
        self.initUI()

    def initUI(self):
        self.lightsBtn = QPushButton('Turn On')
        self.lightsBtn.setCheckable(True)  
        self.lightsBtn.setStyleSheet("QPushButton:checked {color: white; background-color: green;}")
        self.lightsBtn.clicked.connect(self.startLightsThread)

        self.setCentralWidget(self.lightsBtn)    

    def startLightsThread(self):
        print 'start lightsThread'
        self.senderThread = QThread()
        self.senderWorker = SenderWorker()
        self.senderWorker.moveToThread(self.senderThread)
        self.senderThread.started.connect(self.senderWorker.work)
        self.senderThread.start()

        self.receiverThread = QThread()
        self.receiverWorker = ReceiverWorker()
        self.receiverWorker.connectSlots(self.senderWorker)
        self.receiverWorker.moveToThread(self.receiverThread)
        self.receiverThread.start()



class SenderWorker(QObject):

    temperatures = pyqtSignal(object)

    def __init__(self):
        QObject.__init__(self)
        self._mutex = QMutex()
        self._running = True

    @pyqtSlot()
    def work(self):
        while self._running:
            print 'Sender working'
            self.temperatures.emit('123')
            time.sleep(1)



class ReceiverWorker(QObject):

    def __init__(self):
        QObject.__init__(self)
        self._mutex = QMutex()
        self._running = True

    def connectSlots(self, sender):
        self.connect(sender, SIGNAL('temperatures'), self.work(temperatures))

    def work(self, temperatures):
        print 'Receiver working: ', temperatures



app = QApplication(sys.argv)
window = Screen()
window.show()
sys.exit(app.exec_())

更新#1

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import time



class Screen(QMainWindow):
    def __init__(self):
        super(Screen, self).__init__()
        self.initUI()

    def initUI(self):
        self.lightsBtn = QPushButton('Turn On')
        self.lightsBtn.setCheckable(True)  
        self.lightsBtn.setStyleSheet("QPushButton:checked {color: white; background-color: green;}")
        self.lightsBtn.clicked.connect(self.startLightsThread)

        self.setCentralWidget(self.lightsBtn)    

    def startLightsThread(self):
        print 'start lightsThread'
        self.senderThread = QThread()
        self.senderWorker = SenderWorker()
        self.senderWorker.moveToThread(self.senderThread)
        self.senderThread.started.connect(self.senderWorker.work)
        self.senderThread.start()

        self.receiverThread = QThread()
        self.receiverWorker = ReceiverWorker()
        self.receiverWorker.connectSlots(self.senderWorker)
        self.receiverWorker.moveToThread(self.receiverThread)
        self.receiverThread.start()



class SenderWorker(QObject):

    temperatures = pyqtSignal(object)

    def __init__(self):
        QObject.__init__(self)
        self._mutex = QMutex()
        self._running = True

    @pyqtSlot()
    def work(self):
        while self._running:
            print 'Sender working'
            self.temperatures.emit('123')
            time.sleep(1)



class ReceiverWorker(QObject):

    def __init__(self):
        QObject.__init__(self)
        self._mutex = QMutex()
        self._running = True

    def connectSlots(self, sender):
        sender.temperatures.connect(self.work)

    def work(self, temperatures):
        print 'Receiver working: ', temperatures



app = QApplication(sys.argv)
window = Screen()
window.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

问题在于连接,如果要使用旧样式,则必须使用以下语法:

def connectSlots(self, sender):
    self.connect(sender, SIGNAL('temperatures(PyQt_PyObject)'), self.work)

new style中更简单({sender}.{signal}.connect({slot})):

def connectSlots(self, sender):
    sender.temperatures.connect(self.work)