2017-02-27 76 views
0

如何从我的类ScanQrCode()发回信号给calss MainDialog()?我使用Python 2.7和PyQt与QtDesigner4生成的窗口。 我确实设法将类ScanQrCode()中的信号发送到类中的接收函数。然而,当我试图通过另一个类MainDialog(接收它的信号丢失),但在同一个文件PyQt从类(子窗口)发送信号返回到MainWindow(类)

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 

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

回答

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后。