2011-06-09 31 views
1

我有我的GUI和欲estamblish兩級信號PyQt4中

. 
    . 
    . 
    mainWidget = QtGui.QWidget() 
    mainWidget.setLayout(mainLayout) 
    self.setCentralWidget(mainWidget) 
    self.show() 

    """  Creating class  """ 
    self.server = MCCommunication.MCCommunication() 
    self.connect(self.server, QtCore.SIGNAL("textUpdated"), self.insertText); 
    sys.exit(self.app.exec_()) 

的MCCommunication類之間一些通信是以下內容:

類MCCommunication(QtCore.QObject): ''」 classdocs ''」

def __init__(self): 
    ''' 
    Constructor 
    ''' 
    HOST, PORT = socket.gethostbyname(socket.gethostname()), 31000 
    self.server = SocketServer.ThreadingTCPServer((HOST, PORT), MCRequestHandler) 
    ip, port = self.server.server_address 

    # Start a thread with the server 
    # Future task: Make the server a QT-Thread... 
    self.server_thread = threading.Thread(target = self.server.serve_forever) 
    self.server_thread.start() 
    self.emit(QtCore.SIGNAL("textUpdated"), ("TCPServer listening on")) 

,但我得到了以下錯誤:

self.emit(QtCore.SIGNAL("textUpdated"), ("TCPServer listening on")) 
RuntimeError: underlying C/C++ object has been deleted 

回答

2

我不使用老式的信號和插槽語法。
可以使用新的風格:

class MCCommunication(QtCore.QObject): 
    textUpdated = pyqtSignal(str) 
    def __init__(self): 
     super(MCCommunication,self).__init__() 
     ... 
     self.textUpdated.emit("TCPServer listening on") 

在GUI實例:

self.server.textUpdated.connect(self.insertText) 

更新:我加入了斯蒂芬·特里的建議。

P.S. (「TCPServer監聽」)不是一個元組。它缺少逗號。
(「偵聽的TCPServer」,)是一個元素的元組。

4

您需要初始化MCCommunication類中的基礎QObject。將此線添加到__init__方法的開頭:

super(MCCommunication,self).__init__() 
+0

+1。永遠不要忘記在繼承和重寫'__init__'時調用超級'__init__'方法。 – Jeannot 2011-06-10 08:03:15