How to pass string value from dialog to mainwindow
I'm new to python and qt, but in the past I did some programming in java and android. I created before a lot of years an application in java, which I use still and I'm trying to recreated now with python and pyqt5.
I'm using eric IDE and I have created 2 forms, a mainwindow and a dialog so far.
eric generates .py files for the forms, so I have separate files for the mainwindow form and the dialog form. I have already imported the dialog form to the mainwindow form successfully and I can open the dialog form by click on a button.
I have some radioboxes in dialog form and I would like when I click on one of them to update a label' s text on mainwindow form. I have already generated code for onClick action for all the radioboxes and store the value to a string when any of them clicked. But I don't know how to also set this value as mainwinodw' s label text. I searched about this and I found some answers about store to a global variable. I tried that, but I get error when I'm trying to import the mainwindow form's class to the dialog form file.
The debugged program raised the exception unhandled ImportError "cannot import name 'MainWindow
So I cannot connect the dialog form to mainwindow form to somehow pass the data.
I have both of forms in an ui subdirectory and I tried all these:
from .mainwindow import MainWindow from ui.mainwindow import MainWindow from mainwindow import MainWindow
but always I get the above error when I'm trying to start the application.
So the main problem now is how to connect the dialog to the mainwindow and then how to pass the data.
Hi and welcome to devnet,
Don't use global variable for that. You can either use signals and slots or simply have your dialog provide getters to retrieve the values you want.
"use signals and slots": in the dialog, use
emit()to raise a signal once the value has been set. In your main window module, use
connect()to register a slot defined there which will receive the string passed in the signal by the dialog.
"have your dialog provide getters to retrieve the values you want": from main window create your dialog instance, then execute it, then while the dialog variable is still in scope you can call functions in it to retrieve values:
dlg = QDialog(self) if dlg.exec(): value = dlg.someFunction()
In this case, do not set the Qt "auto delete dialog object after executing" flag (it is not normally switched on).
Either method is acceptable, with pros & cons. Both are preferable to using a "global variable" (which you will find pretty difficult/non-obvious in Python anyway).