![](/img/trans.png)
[英]PYQT: How to send a custom signal from the mainwindow(parent) to a child window?
[英]PyQt Send a signal from a class (sub-window) back to the MainWindow (class)
如何將信號從類ScanQrCode()發送回calss MainDialog()? 我正在將QtDesigner4生成的窗口與Python 2.7和PyQt一起使用。 我確實設法將信號從類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
沒有錯誤。 只是主窗口不會更新。 我認為原則上信號和連接都可以正常工作,但是一定有一些看不見的東西。 任何幫助,將不勝感激。
看起來您顯示的ScanQrCode
實例( windowScanQrCode
)從未將其trigger
信號連接到updateQrCode
。 同時,永遠不會顯示您正確連接的ScanQrCode
實例( thread
)。
最簡單的解決方案是刪除方法scanQrCode
並替換該行
self.pushButton_ScanQrCode.clicked.connect( self.scanQrCode )
與線
self.pushButton_ScanQrCode.clicked.connect( self.thread.show )
然后在定義thread
之后調用connectActions()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.