2016-12-16 123 views
0

有兩個獨立的QT Designer UI格式。兩個按鈕都有。當點擊form1中的按鈕時,form2打開。點擊form2中的按鈕時如何向form1發送消息。一切正常,但消息。兩個獨立QWidget之間的通信

form1.py

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import form2       #Import for Signal 
from form2 import *      # Import for UI 
RegForm = "regform.ui" 
Ui_RegForm, QtBaseClass = uic.loadUiType(RegForm) 
class RegForm(QtGui.QMainWindow, Ui_RegForm): 
    def __init__(self,parent = None): 
     super(RegForm,self).__init__(parent,flags =     Qt.WindowCloseButtonHint) 
     self.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
     self.setupUi(self) 

     self.unHideBtn.clicked.connect(self.showUnhideForm) 

     self.myForm = form2.UnhideForm(self)     #For Signal 
     self.myForm.mySignal.connect(self.receiveSignal)  #Signal 

    def receiveSignal(self, message): 
     QtGui.QMessageBox.information(self, 'Message', message) 

    def showUnhideForm(self): 
     sub = UnhideForm(self) 
     sub.show() 

form2.py

from PyQt4 import QtCore, QtGui, uic 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
UnhideForm = "unhide.ui" 
Ui_UnhideForm, QtBaseClass = uic.loadUiType(UnhideForm) 
class UnhideForm(QtGui.QMainWindow, Ui_UnhideForm): 
    mySignal = QtCore.pyqtSignal(str)#Signal 
    def __init__(self,parent = None): 
     super(UnhideForm,self).__init__(parent,flags = Qt.WindowCloseButtonHint) 
     self.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
     self.setupUi(self) 

     # On clicking emit a signal 
     self.quitBtn.clicked.connect(self.emitSignal) 

     # And Close the form 
     self.quitBtn.clicked.connect(self.close) 
    def emitSignal(self): 
     if(not self.signalsBlocked()): 
      self.mySignal.emit("Yes") 

窗體2正常打開,當quitBtn被cliked關閉。但是沒有消息被髮送回Form1。我是PyQt的新手。請幫忙。謝謝。

回答

0
#This is the example code for sent a message to one UI to another UI 
#If your are not expecting this answer, sorry. 
#Thanks, Subin 

import sys 
from PyQt4 import QtGui 
from PyQt4 import QtCore 

class FORM1 (QtGui.QWidget): 

    def __init__(self, parent=None): 
     super(FORM1, self).__init__(parent) 

     self.button1 = QtGui.QPushButton (self) 
     self.button1.setGeometry (QtCore.QRect(10, 10, 100, 30)) 
     self.button1.setObjectName ('button1') 
     self.button1.setText ('Button1')   

     self.textEdit = QtGui.QTextEdit(self) 
     self.textEdit.setGeometry (QtCore.QRect(10, 50, 400, 100)) 
     self.textEdit.setObjectName('textEdit')   

     self.resize(500, 200) 

     self.button1.clicked.connect (self.callForm2)  


    def callForm2 (self) : 
     self.form2_widget  = FORM2() 
     self.form2_widget.show ()    
     self.form2_widget.button2.clicked.connect (self.sendMessage)  

    def sendMessage (self) :   
     self.textEdit.setText ('There are two separate QT Designer ui forms.Both have two buttons.')  



class FORM2 (QtGui.QWidget) : 

    def __init__(self, parent=None): 
     super(FORM2, self).__init__(parent)   

     self.button2 = QtGui.QPushButton (self) 
     self.button2.setGeometry(QtCore.QRect(50, 50, 100, 30)) 
     self.button2.setObjectName ('Button2') 
     self.button2.setText ('Button2')   

     self.resize(400, 100) 


if __name__=='__main__': 
    app = QtGui.QApplication(sys.argv) 
    widget = FORM1() 
    widget.show() 
    app.exec_() 
+0

哇,它的工作原理。謝謝蘇賓。非常感謝您 –

+0

如果這解決了您的問題,我們鼓勵您接受答案。我希望對所做的回答做一點解釋。 – mdurant