简体   繁体   中英

How to access a widget in a QMainWindow from a QDialog

Before posting my question I searched a lot about it and I found some questions that might be similar but they do not solve my problem. I believe it is quite easy but I don't know how:

below is a minimal example of the problem:

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())

Could you please tell me how can I implement writeHello() method in order to write something in firstLineEdit in the MainWindow

Thanks

First of all you should not modify the code generated by Qt Designer since it is not a GUI, it is just a class that fills a GUI, and that brings several inconveniences such as not being able to overwrite the methods of the widget, or some you want to use methods of the widget in that class. Instead it inherits from a suitable widget and uses the other class as an interface.

Going to the point you should not mix the classes since there will be a lot of dependency between them and in the future if you modify one class you will have to modify the other which is unbeatable, instead you use the signals to notify any change or action.

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM