PyQT4 - 两个窗口之间的通信

时间:2012-05-18 08:38:28

标签: python pyqt4 communication

我在PyQt中的两个窗口之间的通信存在问题。

主窗口= UI_Form(类MyForm) 附加窗口= UI_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中出现了问题。第一行和第二行被忽略。我不知道为什么。

1 个答案:

答案 0 :(得分:2)

在您的方法Employee.addText中,您可以创建一个新的MyForm。这可能不是你想要的。您可以通过myappEmployee内部访问原始self.parentWidget()

class Employee(QtGui.QMainWindow):

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