PyQt4信号不发射

时间:2014-09-17 07:21:57

标签: python qt

我尝试构建一个udp服务器来接收二进制消息,套接字在收到消息时发出processMsg信号,processMsg函数尝试根据消息类型发出不同的信号。 QDefines对象定义要生成的消息类型和信号。我使用dict解决python中丢失的开关/案例。问题是当收到UCSI_SET_RF_RSP_E消息时,setRfRsp函数没有执行。

Main.py文件:

class mainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
    super(mainWindow, self).__init__()
    self.ui = Ui_MainWindow()
    self.defines = QDefines()
    self.connect(self.defines,QtCore.SIGNAL("signalSetRfRsp(PyQt_PyObject)"), self.setRfRsp)
    self.socket = QUdp(self.localIp, self.localPort, self.remoteIp, self.remotePort)
    self.connect(self.socket, QtCore.SIGNAL("processMsg(int,PyQt_PyObject)"), self.processMsg)
def setRfRsp(self, msg):
    if msg == 0x00000000:
        print "open"
    else:
        print "closed"
def processMsg(self, msgType, msg):
    defines = QDefines()
    msg_dict = defines.msgDictGen();
    msg_dict[msgType](msg)

defines.py文件:

class QDefines(QtCore.QObject):
UCSI_SET_RF_RSP_E = 0x000d
def __init__(self, parent = None):
    super(QDefines, self).__init__()

def UCSI_SET_RF_RSP(self, msg):
    self.emit(QtCore.SIGNAL("signalSetRfRsp(PyQt_PyObject)"), msg) 

def msgDictGen(self):
    self.msgDict = {
        self.UCSI_SET_RF_RSP_E : self.UCSI_SET_RF_RSP
    } 
    return self.msgDict

1 个答案:

答案 0 :(得分:0)

发出信号的QDefines实例从未将任何信号连接到任何信号,并且只有在processMsg返回时才会收集垃圾。

也许你打算写:

    def processMsg(self, msgType, msg):
        msg_dict = self.defines.msgDictGen()
        msg_dict[msgType](msg)

您还应该考虑摆脱那种令人讨厌的旧式信号语法,并使用漂亮,干净的new-style代替:

class QDefines(QtCore.QObject):
    signalSetRfRsp = QtCore.pyqtSignal(object)
    ...

    def UCSI_SET_RF_RSP(self, msg):
        self.signalSetRfRsp.emit(msg) 

class mainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        ...
        self.defines = QDefines()
        self.defines.signalSetRfRsp.connect(self.setRfRsp)

另外,我建议你忘记尝试在python中复制switch语句,而只是使用if/elif。在此之前,您需要大量的分支才能成为重要的性能问题。