2012-05-18 150 views
1

我在PyQt的兩個窗口之間的通信出現問題。PyQT4 - 兩個窗口之間的通信

主窗口= UI_Form(類MyForm的) 附加窗口= UI_Employee(Employee類)

我想,當我點擊AddTextButton(Ui_Employee),設置文本LineTextEdit(UI_Form) 這是我的碼。

import sys 
from PyQt4 import QtCore, QtGui 

from Form import Ui_Form 
from Window import Ui_Employee 

class MyForm(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.ui = Ui_Form() 
     self.ui.setupUi(self) 

     QtCore.QObject.connect(self.ui.AddButton,QtCore.SIGNAL("clicked()"), self.add) 

    def add(self): 
     self.Employee = Employee(self) 
     self.Employee.show() 


class Employee(QtGui.QMainWindow): 
    def __init__(self,parent=None): 
     QtGui.QWidget.__init__(self,parent) 
     self.ui = Ui_Employee() 
     self.ui.setupUi(self) 

     QtCore.QObject.connect(self.ui.AddRowButton,QtCore.SIGNAL('clicked()'), self.addText) 

    def addText(self): 
     self.Form = MyForm() 
     self.Form.ui.textEdit.setText('someText') 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    myapp = MyForm() 
    myapp.show() 
    sys.exit(app.exec_()) 

我在addText方法中遇到了問題。第一行和第二行被忽略。我不知道爲什麼。

回答

2

在您的方法Employee.addText中,您將創建一個新的MyForm。這可能不是你想要的。您可以從Employee內部通過self.parentWidget()訪問原始myapp

class Employee(QtGui.QMainWindow): 

    def addText(self): 
     self.parentWidget().ui.textEdit.setText('someText')