如何从QDialog访问QMainWindow中的小部件

时间:2019-03-19 04:20:19

标签: python python-3.x pyqt pyqt5

在发布我的问题之前,我进行了很多搜索,发现一些可能相似的问题,但它们不能解决我的问题。我相信这很容易,但是我不知道怎么做:

以下是该问题的一个最小示例:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        #MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(574, 521)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
        self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.firstPushButton.clicked.connect(self.showDialog)

        # the other stuff related to layout setup is ommited

    def showDialog(self):
        dialog = MyDialog(MainWindow)
        dialog.exec()

class MyDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedSize(400, 200)
        self.myButton = QtWidgets.QPushButton("Write something")
        # When I click the myButton, I want it to change the text of MainWindow lineEdit
        self.myButton.clicked.connect(self.writeHello)

    def writeHello(self):
      # How can I access firstLineEdit from MainWindow? I want to write "Hello" to the firstLineEdit
      pass

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.setWindowTitle("BEM Analysis for propellers")
    MainWindow.show()
    sys.exit(app.exec())

能否请您告诉我如何实现writeHello()方法以便在MainWindow中的firstLineEdit中编写内容

谢谢

1 个答案:

答案 0 :(得分:4)

首先,您不应该修改Qt Designer生成的代码,因为它不是GUI,它只是一个填充GUI的类,并且带来了一些不便,例如无法覆盖小部件的方法。 ,或者您想使用该类中的窗口小部件的方法。取而代之的是,它继承自合适的小部件,并使用另一个类作为接口。

至此,您不应该混合使用这些类,因为它们之间会存在很多依赖关系,将来如果您修改一个类,则必须修改另一个无与伦比的类,而是使用信号来通知任何更改或动作。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        #MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(574, 521)
        MainWindow.setWindowIcon(QtGui.QIcon(':/icons/drone.ico'))
        MainWindow.setIconSize(QtCore.QSize(32, 32))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        MainWindow.setCentralWidget(self.centralwidget)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
        self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.verticalLayout_2.addWidget(self.firstPushButton)
        self.verticalLayout_2.addWidget(self.firstLineEdit)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.firstPushButton.clicked.connect(self.showDialog)

    def showDialog(self):
        dialog = MyDialog()
        dialog.clicked.connect(self.writeHello)
        dialog.exec()

    @QtCore.pyqtSlot()
    def writeHello(self):
        self.firstLineEdit.setText('Hello')

class MyDialog(QtWidgets.QDialog):
    clicked = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedSize(400, 200)
        self.myButton = QtWidgets.QPushButton("Write something")
        # When I click the myButton, I want it to change the text of MainWindow lineEdit
        self.myButton.clicked.connect(self.clicked)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.myButton)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.setWindowTitle("BEM Analysis for propellers")
    w.show()
    sys.exit(app.exec())