Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

get a value from secondary window and use it in main window



  • I have two windows. when I click a pushbutton in GUIWindow, it opens the settings window. The attribute 'new' is populated in the Settings_window. Now, I want the value stored in the attribute 'new' in class Settings_window to be used in the class GUIWindow. How do I do that? below is the code I wrote but when I run this I get an error saying Settings_window has no attribute 'new'. Can someone please help me resolve this issue.

    device_id_lm_x = 4
    device_id_lm_y = 5
    device_id_lm_z = 6
    class Settings_window(qtw.QWidget):
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.sw = Ui_settings()
            self.sw.setupUi(self)
    
            self.sw.radio_lm_x_button.clicked.connect(self.choose_device_id)
            self.sw.radio_lm_y_button.clicked.connect(self.choose_device_id)
            self.sw.radio_lm_z_button.clicked.connect(self.choose_device_id)
    
            self.sw.set_button.clicked.connect(lambda: self.set_border(self.device_id))
    
        def choose_device_id(self):
            if self.sw.radio_lm_x_button.isChecked():
                self.device_id = device_id_lm_x
                print(self.device_id)
            elif self.sw.radio_lm_y_button.isChecked():
                self.device_id = device_id_lm_y
                print(self.device_id)
            elif self.sw.radio_lm_z_button.isChecked():
                self.device_id = device_id_lm_z
                print(self.device_id)
    
        def set_border(self, device_id):
            self.new = 1234
        
    class GUIWindow(qtw.QWidget):
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.ui = Ui_Form()
            self.ss = Settings_window()
            self.ui.setupUi(self)
    
            self.ui.new_button.clicked.connect(lambda: self.new_val(self.ss.new))
        
        def new_val(self, value):
            print(value)

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Until set_border has been called, Settings_window has not attribute named new.



  • @SGaist Hi, I want the value in the attribute name 'new' that is populated in set_border method. That is where my attribute gets its value and I want to use that value in the main window.


  • Lifetime Qt Champion

    That I understood. However, the way you implemented it can't work.

    When you click on "new_button", you are directly requesting the value of new from your Settings_window object which does not exists since it's only created in set_border.



  • @SGaist How else can I get the value from the attribute 'new' in the GUIWindow. can you please show me an example


  • Lifetime Qt Champion

    @Praanesh said in get a value from secondary window and use it in main window:

    How else can I get the value from the attribute 'new' in the GUIWindow

    Set self.new in constructor. It's really that simple...


Log in to reply