PyQt从类(子窗口)向主窗口(类)发送信号

2024-05-04 23:21:56 发布

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

如何将信号从类ScanQrCode()发送回calss MainDialog()?我使用的是python2.7和PyQt与QtDesigner4生成的windows。 我确实设法将信号从类ScanQrCode()发送到类中的一个接收函数。但是,当我试图通过另一个类MainDialog()接收信号时,信号会丢失,但在同一个文件中

class ScanQrCode( QtGui.QDialog, Ui_ScanQrCode ):
    trigger = QtCore.pyqtSignal()    

    def __init__( self, parent = None ):
        super( ScanQrCode, self ).__init__( parent )
        self.setupUi( self ) 
        self.pushButton_Scan.clicked.connect( self.scan )         
    # End of __init__    

    def scan( self ):
        print 'Scanning'
        # Place holder for the functionality to scan the QR code
        self.lineEdit_QrCode.setText( "QR-123456789" ) # Dummy QR code
        if ( not self.signalsBlocked() ):
            print 'emit trigger'
            self.trigger.emit()
    # End of sub-function scan
# End of class ScanQrCode

class MainDialog( QtGui.QMainWindow, agpt_gui.Ui_MainWindow ):
    def __init__( self, parent = None ):
        super( MainDialog, self ).__init__( parent )
        self.setupUi( self )          
        self.connectActions()
        self.windowScanQrCode = None

        #Define threads
        self.thread = ScanQrCode()
        self.thread.trigger.connect( self.updateQrCode )
    # end of __init__

    def main( self ):
        self.show()  

    def connectActions( self ):
        # Define the connection from button to function
        self.pushButton_ScanQrCode.clicked.connect( self.scanQrCode ) 
        self.pushButton_Exit.clicked.connect( self.exit )
    # End of sub-function connectActions


    @QtCore.pyqtSlot()    
    def updateQrCode( self ):
        """
        Update the new scanned QR code in the main window
        """
        print 'Update QR code'
        self.lineEdit_QrCode.setText("123456789")
    # End of sub-function updateQrCode

    def scanQrCode( self ):
        if self.windowScanQrCode is None:
            self.windowScanQrCode = ScanQrCode( self )  
        self.windowScanQrCode.show()
    # End of sub-function scanQrCode
# End of class MainDialog

没有错误。只是主窗口不更新。 我认为原则上信号和连接正常,但一定有什么东西我看不见。 任何帮助都将不胜感激。在


Tags: oftheselfnone信号initdeffunction
1条回答
网友
1楼 · 发布于 2024-05-04 23:21:56

看起来您显示的ScanQrCode实例(windowScanQrCode)从未将其trigger信号连接到updateQrCode。同时,您正确连接的ScanQrCode实例(thread)永远不会显示。在

最简单的解决方案是删除方法scanQrCode并替换行

self.pushButton_ScanQrCode.clicked.connect( self.scanQrCode )

用绳子

^{pr2}$

然后在定义thread之后调用connectActions()。在

相关问题 更多 >