PyQt将类(子窗口)中的信号发送回MainWindow(类)

时间:2017-02-27 19:06:51

标签: python pyqt

如何从我的类ScanQrCode()发送信号回到calss MainDialog()?我使用Python 2.7和PyQt与QtDesigner4生成的窗口。 我确实设法将类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

没有错误。只是主窗口不会更新。 我认为原则上信号和连接正在工作但是必须有一些我看不到的东西。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

看起来您展示的ScanQrCode个实例(windowScanQrCode)从未将trigger信号连接到updateQrCode。同时,从未显示正确连接的ScanQrCode实例(thread)。

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

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

self.pushButton_ScanQrCode.clicked.connect( self.thread.show ) 

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