2012-05-18 147 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')