PyQT4中的信号

2024-10-02 22:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有igui,我想在两个类之间建立一些通信

    .
    .
    .
    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类如下:

类通信(QtCore.QObject): ''' 类文档 '''

^{pr2}$

但我得到了以下错误:

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

Tags: selfcreatingsignalservershowqtguiqwidgetqtcore
2条回答

我不使用老式的Signal和slot语法。
您可以使用新样式:

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

在GUI实例中:

^{pr2}$

更新:我添加了斯蒂芬特里的建议。在

p.S.(“TCPServer监听”)不是元组。它没有逗号。
(“TCPServer监听”)是一个单元素元组。在

您需要初始化MCCommunication类中的底层QObject。将此行添加到__init__方法的开头:

super(MCCommunication,self).__init__()

相关问题 更多 >