Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I access the data from a QLineEdit of ui_dialog class ( my_dialog.py) to another class Ui_MainWindow (main_window.py)
Qt 6.11 is out! See what's new in the release blog

How can I access the data from a QLineEdit of ui_dialog class ( my_dialog.py) to another class Ui_MainWindow (main_window.py)

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 2.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AkritA Offline
    AkritA Offline
    Akrit
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • AkritA Offline
        AkritA Offline
        Akrit
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • AkritA Offline
            AkritA Offline
            Akrit
            wrote on last edited by Akrit
            #5

            Example.py

            from PyQt5 import QtCore, QtGui, QtWidgets

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

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

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

            1 Reply Last reply
            0
            • AkritA Offline
              AkritA Offline
              Akrit
              wrote on last edited by
              #6

              Example_dialog.py

              from PyQt5 import QtCore, QtGui, QtWidgets

              class Ui_Dialog(object):
              def setupUi(self, Dialog):
              Dialog.setObjectName("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.lineEdit.setObjectName("lineEdit")
              self.btn_ok = QtWidgets.QPushButton(Dialog)
              self.btn_ok.setGeometry(QtCore.QRect(50, 80, 93, 28))
              self.btn_ok.setObjectName("btn_ok")
              self.btn_cancel = QtWidgets.QPushButton(Dialog)
              self.btn_cancel.setGeometry(QtCore.QRect(160, 80, 93, 28))
              self.btn_cancel.setObjectName("btn_cancel")

                  self.retranslateUi(Dialog)
                  self.btn_ok.clicked.connect(Dialog.accept)
                  self.btn_cancel.clicked.connect(Dialog.reject)
                  QtCore.QMetaObject.connectSlotsByName(Dialog)
              
              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()
              ui.setupUi(Dialog)
              Dialog.show()
              sys.exit(app.exec_())

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • AkritA Offline
                  AkritA Offline
                  Akrit
                  wrote on last edited by
                  #8

                  https://github.com/akritsingha/Pyqt5

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • AkritA Offline
                      AkritA Offline
                      Akrit
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved