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

用户名

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

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

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()方法以便firstLineEdit在MainWindow中写一些东西吗?

谢谢

永乐

首先,您不应该修改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())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章