Search code examples
pythonclassvariablesparent-childinstance-variables

Alternative way for storing (QDialog) variables, even when they close - from any Class (Parenting??)


I'm trying to figure out a better way of coding in Python. (Only have been coding for a few weeks.)

Summary: Finding a better way to keep (and access) variable values entered in a closing dialog box (that's called within another class). Must be able to use these values from either class.

I used QtDesigner for making a main window and a dialog box. Once I click on the "Settings" menu from the main window i open "MyDialog". There I have some settings that can be changed. I also have a "Save" button on the dialog for saving my settings. Here is an example code:

up = down = left = right = 0

class MainForm(QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)
        self.ui = Ui_MainForm()
        self.ui.setupUi(self)

        self.ui.actionSettings.triggered.connect(self.open_my_dialog)

    def open_my_dialog(self):
        my_dialog = MyDialog()
        my_dialog.exec_()


class MyDialog(QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        self.ui = Ui_MyDialog()
        self.ui.setupUi(self)

        self.ui.pushButton_save.clicked.connect(self.save_dialog)

        self.ui.spinBox_up.setValue(up)
        self.ui.spinBox_down.setValue(down)
        self.ui.spinBox_left.setValue(left)
        self.ui.spinBox_right.setValue(right)

    def save_dialog(self):
        global up, down, left, right

        up = self.ui.spinBox_up.value()
        down = self.ui.spinBox_down.value()
        left = self.ui.spinBox_left.value()
        right = self.ui.spinBox_right.value()
        self.close()

I need to be able to access the values entered in the dialog box once it has been closed from either of the classes (MainForm or by re-entering the dialog again; MyForm() and MyDialog()). As you can see the way I'm keeping the settings in cache is by assigning them to global variables. I know that this is not the proper way of doing it. But I need to keep the settings saved once the "Save" button is clicked, so that if I re-open the dialog it will show me the settings I last left there (when I clicked on save). Also I have to use these same values from my MainForm() class. I read a lot regarding calling class and method variables on other classes. Also parenting (this one got me a bit confused). But the examples were very simplistic. I understand it if I write the class from scratch like the ones they give you. But since I am using the designer for Ui i have a (way of) different code (lots of self.ui.xxx(xxselfxx) stuff all around (also the init is way different than the examples online). I tried and searched alot for an answer but gave up and solved it with global variables.

If some one would kindly explain to me how the proper (Pythonic) way of solving this case should be I will be very greatful.

Thanks!


Solution

  • After hours of testing and research I found a better approach for my coding. Instead of using the "frowned upon" global variables as I had before, I now can accomplish my code using class variables. Also I tried to make it work with instance variables but unfortuntely it didn't work no matter what i did. The answer I'm providing is more Pythonic/proper way.. I'm sure of that. But it doesn't mean it's the best way. I'm still open for suggestions. If you believe it can be written better (instance variables perhaps?) please by all means share.

    Here you go:

    class MainForm(QMainWindow):
        def __init__(self, parent=None):
            super(MainForm, self).__init__(parent)
            self.ui = Ui_MainForm()
            self.ui.setupUi(self)
    
            self.ui.actionSettings.triggered.connect(self.open_my_dialog)
    
        def open_my_dialog(self):
            my_dialog = MyDialog()
            print("Test:", MyDialog.up, MyDialog.down, MyDialog.left, MyDialog.right)  # for testing the values from this class
            my_dialog.exec_()
    
    
    class MyDialog(QDialog):
        up = down = left = right = 0
        def __init__(self, parent=None):
            super(MyDialog, self).__init__(parent)
            self.ui = Ui_MyDialog()
            self.ui.setupUi(self)
    
            self.ui.pushButton_save.clicked.connect(self.save_dialog)
    
            self.ui.spinBox_up.setValue(MyDialog.up)
            self.ui.spinBox_down.setValue(MyDialog.down)
            self.ui.spinBox_left.setValue(MyDialog.left)
            self.ui.spinBox_right.setValue(MyDialog.right)
    
        def save_dialog(self):
            MyDialog.up = self.ui.spinBox_up.value()
            MyDialog.down = self.ui.spinBox_down.value()
            MyDialog.left = self.ui.spinBox_left.value()
            MyDialog.right = self.ui.spinBox_right.value()
            self.close()
            return MyDialog.up, MyDialog.down, MyDialog.left, MyDialog.right