Important: Please read the 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.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
            elif self.sw.radio_lm_y_button.isChecked():
                self.device_id = device_id_lm_y
            elif self.sw.radio_lm_z_button.isChecked():
                self.device_id = device_id_lm_z
        def set_border(self, device_id):
   = 1234
    class GUIWindow(qtw.QWidget):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.ui = Ui_Form()
   = Settings_window()
            self.ui.new_button.clicked.connect(lambda: self.new_val(
        def new_val(self, 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 in constructor. It's really that simple...

Log in to reply