我编写了一个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为什么会发生这种事,我能做些什么来解决它呢?
提前谢谢。
编辑:
备注-1:
dummyFunc以前工作过,但它不适用于lambda:self.dummyFunc()
备注-2:
我以为lambda:self.shouldGiveError()会出错,因为没有这样的函数,但是我什么也没有得到。
这是我使用lambda的方式的问题吗?
发布于 2011-11-28 20:41:35
你看过QObject.invokeMethod()吗?
自PyQt 4.4以来就支持它,当没有匹配的信号时,它应该允许您调用带有参数的时隙。
https://stackoverflow.com/questions/8298980
复制相似问题