我编写了一个pyQt客户机-服务器应用程序。(python:3.2.2, pyQT:4.8.6)
发送方向侦听的接收方发送消息,接收方向发送方发送响应。我不希望响应立即发送,但经过一个小延迟。
这是接收者代码的一部分:
----------------------------- msghandler.py-----------------------------------
class MsgHandler(QObject):
def __init__(self):
QObject.__init__(self)
self.mSec1Timer = None
def setParentUI(self, p):
self.parentUI = p
def handle_ask(self, ID, stamp, length, cargo, peerSocket):
print("Incoming:ASK")
#self.mSec1Timer.timeout.connect(lambda:self.send_msg_reply(peerSocket))
self.mSec1Timer.timeout.connect(self.dummyFunc) #Works
self.mSec1Timer.timeout.connect(lambda:self.dummyFunc())#See Rem-1
self.mSec1Timer.timeout.connect(lambda:self.shouldGiveError())#See Rem-2
self.parentUI.timerStart.emit(5)
@pyqtSlot(tuple)
def send_msg_reply(self, peerSocket):
print("This is not printed")
self.mSec1Timer.timeout.disconnect()
@pyqtSlot()
def dummyFunc(self):
print("dummy @ ",QDateTime.currentMSecsSinceEpoch())
self.mSec1Timer.timeout.disconnect()
------------------------------------------------------------------------------
from msghandler import *
class DialogUIAgent(QDialog):
timerStart = pyqtSignal(int)
def __init__(self):
QDialog.__init__(self)
self.myHandler = MsgHandler()
self.myHandler.setParentUI(self)
self.myTimer = QTimer()
self.myTimer.setSingleShot(True)
self.myHandler.mSec1Timer = self.myTimer
self.timerStart.connect(self.startMyTimer)
@pyqtSlot(int)
def startMyTimer(self, msec):
self.myTimer.start(msec)
首先测试行为,我使用self.mSec1Timer.timeout.connect(self.dummyFunc)
,输出如预期:
Incoming:ASK
dummy @ 1322491256315
Incoming:ASK
dummy @ 1322491260310
Incoming:ASK
dummy @ 1322491265319
Incoming:ASK
dummy @ 1322491270323
Incoming:ASK
dummy @ 1322491275331
但是当我使用self.mSec1Timer.timeout.connect(lambda:self.send_msg_reply(peerSocket))
时,插槽从未被调用。输出:
Incoming:ASK
Incoming:ASK
Incoming:ASK
Incoming:ASK
为什么会发生这种情况,我能做些什么来解决它?
提前感谢。
编辑:
Remark-1:
dummyFunc工作之前,但它不工作与
lambda:self.dummyFunc()
Remark-2:
我期待lambda:self.shouldGiveError()
的错误,因为没有这样的功能,但我什么也没有得到。
这是我使用lambda的方式的问题吗?
您看过QObject.invokeMethod()吗?
自PyQt 4.4以来一直支持它,当你没有匹配的信号时,它应该允许你调用带有参数的插槽。