Important: Please read the Qt Code of Conduct -

How can I access the data from a QLineEdit of ui_dialog class ( to another class Ui_MainWindow (

  • I create a main_window .py and also create my own customize dialog box named where I include a QLineEdit and two QPushButtons. Now the question is how can I fetch the data from the QLineEdit of the to the and also how can I access those buttons from the

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Usually by giving a proper API to the widget so you don't expose internal details.

    What do you want to access these buttons from your MainWindow ?

  • Simply I just want to access the objects of a class from another class . For example, If a user enters the text of a lineEdit object in another window (ui_dialog), then I want the text of the object in my main_window (ui_mainWindow).

  • Lifetime Qt Champion

    That's clear and for that part, I already suggested to give your widget a proper API rather than trying to access its internals.

    It's the access to the buttons that is intriguing me.


    from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.resize(400, 400)
    MainWindow.setMinimumSize(QtCore.QSize(400, 400))
    MainWindow.setMaximumSize(QtCore.QSize(400, 400))
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(100, 60, 211, 81))
    font = QtGui.QFont()
    self.label.setObjectName("label") = QtWidgets.QPushButton(self.centralwidget), 210, 191, 61))
    font = QtGui.QFont()
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 26))

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "TextLabel"))"MainWindow", "Open DialogBox"))

    if name == "main":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()


    from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_Dialog(object):
    def setupUi(self, Dialog):
    Dialog.resize(300, 130)
    Dialog.setMinimumSize(QtCore.QSize(300, 130))
    Dialog.setMaximumSize(QtCore.QSize(300, 130))
    self.lineEdit = QtWidgets.QLineEdit(Dialog)
    self.lineEdit.setGeometry(QtCore.QRect(50, 30, 201, 31))
    self.btn_ok = QtWidgets.QPushButton(Dialog)
    self.btn_ok.setGeometry(QtCore.QRect(50, 80, 93, 28))
    self.btn_cancel = QtWidgets.QPushButton(Dialog)
    self.btn_cancel.setGeometry(QtCore.QRect(160, 80, 93, 28))

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.btn_ok.setText(_translate("Dialog", "OK"))
        self.btn_cancel.setText(_translate("Dialog", "Cancel"))

    if name == "main":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()

  • Lifetime Qt Champion

    These are two designer based widgets. It still does not explain what you are trying to achieve.

    Please, when you post code, use the coding tags to make that readable.

  • Lifetime Qt Champion

    Take a look at the tutorial here.

    This will show you how to better encapsulate your Designer generated widgets. This will also give you a cleaner approach to add the API I already suggested to use above.

    Add a getter to your dialog widget that will return the content of the line edit.

  • Here are two python file. One contains the Ui_MainWindow class and another contains Ui_Dialog class. So, here when I clicked the button of the mainWindow, it opens the dialog box. So, user gives some input in the lineEdit of the dialog box and then clicked ok button. When the user clicked the ok button the dialog box close.

    I want to get the value of the lineEdit value of the dialog box when click the ok button. And also I want to update the label of the mainWindow by that lineEdit value.

  • Lifetime Qt Champion

    That I understood, hence my suggestions to give your dialog a getter to retrieve the line edit content once your dialog is closed as well as use the technique from the tutorial to build a custom widget using your designer generated code.

Log in to reply