Reputation: 65
I have one main window and one dialog in PYQT. I receive the input from user in main window and then open the dialog window and pass the input to this window.
in dialog window again I receive another input and add this new input with the other input which I passed form main window.
class MainWindow (QMainWindow):
def __init__(self):
super().__init__()
self.window=Ui_MainWindow()
self.window.setupUi(self)
def open_qdialog_window(self):
self.dialog_window = Qdialog(self.window.lineedit1.currentText())
self.dialog_window .show()
class Qdialog(QDialog):
def __init__(self,DS_parameter):
super().__init__()
self.DS_parameters=DS_parameters
#UI file of oscilloscope
self.window=Ui_Qdialog()
self.window.setupUi(self)
self.UI()
def summation(self):
self.DS_parameters=self.DS_parameters+self.window.lineedit2.currentText()
Now the question is how I can return self.DS_parameters to the main window and update editline1 with this new value?
Upvotes: 1
Views: 3222
Reputation: 271
To me, it is difficult to provide you with specific (probably more helpful) feedback on your code, since the part you shared is incomplete. However, I hope that my rather general answer is somewhat useful.
In Qt, signals are used to establish connections between GUI elements. The basic idea is that an event triggers the execution of a function. For instance, whenever a button is pressed, some text gets updated. Below you find a working example illustrating how to return the sum to the main window and update the corresponding line.
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QDialog,
QWidget,
QVBoxLayout,
QLineEdit,
QLabel,
QPushButton
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.user_input = QLineEdit()
self.populate()
self.show()
def populate(self):
widgets = [QLabel("Insert a number"), self.user_input]
centralWidget = self.group_widgets(widgets)
self.setCentralWidget(centralWidget)
def group_widgets(self, widgets):
parentWidget = QWidget()
layout = QVBoxLayout()
for widget in widgets: layout.addWidget(widget)
parentWidget.setLayout(layout)
return parentWidget
def when_input(self, function):
self.user_input.textChanged.connect(function)
class Dialog(QDialog):
def __init__(self):
super().__init__()
self.user_input = QLineEdit()
self.admin_input = QLineEdit("0")
self.button = QPushButton("add")
self.relay_sum = None # function to relay result of addition
self.populate()
self.show()
def populate(self):
widgets = self.get_widgets()
layout = self.get_layout(widgets)
self.setLayout(layout)
def get_widgets(self):
widgets = [
QLabel("Inserted number"),
self.user_input,
QLabel("Insert the second number"),
self.admin_input,
self.button
]
return widgets
def get_layout(self, widgets):
layout = QVBoxLayout()
for widget in widgets: layout.addWidget(widget)
return layout
def when_buttonReleased(self, function):
self.relay_sum = function
self.button.released.connect(self.process_input)
def process_input(self):
user_number = float(self.user_input.text())
admin_number = float(self.admin_input.text())
result = "%.2f" %(user_number + admin_number)
self.relay_sum(result)
def main():
app = QApplication([])
mainWindow = MainWindow()
dialog = Dialog()
mainWindow.when_input(lambda text: dialog.user_input.setText(text))
dialog.when_buttonReleased(lambda text: mainWindow.user_input.setText(text))
app.exec_()
if __name__ == "__main__":
main()
As you have noticed, I allowed myself to change variable names you proposed. In the example, two windows are created and two connections are established:
[1. connection] Whenever the text of the main window is changed (MainWindow.user_input.textChanged), the new text is set as the content of the corresponding line in the dialog window (Dialog.user_input.setText).
[2. connection] Whenever the button in the dialog window is released (Dialog.button.released), the values in the dialog window are summed and the result is set as text of the main window (MainWindow.user_input.setText).
Upvotes: 1