Unsolved How can I access the data from a QLineEdit of ui_dialog class ( my_dialog.py) to another class Ui_MainWindow (main_window.py)
-
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?
-
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).
-
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.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_()) -
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_()) -
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.
-
-
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.
-
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.