Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to pass a value from one class to another where each class is on different file
Forum Update on Monday, May 27th 2025

How to pass a value from one class to another where each class is on different file

Scheduled Pinned Locked Moved Unsolved Qt for Python
48 Posts 4 Posters 12.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_hobbyist
    wrote on last edited by john_hobbyist
    #1

    So, the problem is that I have a GUI, the whole code is written on python and PyQt5. I have a button (BT1) where I press it and runs class_1, which exists on file_1. When the class_1 runs and stores a value in var_1 and then finishes. Let's say var_1 = 10. Then I click on another button (BT2) and then class_2 runs, which is stored in a different file (file_2). I need to pass the value from var_1 to var_2. The var_2 is a variable of class_2. I tried global variables but, I cannot pass the value from var_1 to var_2. Any idea how to achieve that? I have also tried many stackoverflow suggestions without any success.

    JonBJ 1 Reply Last reply
    0
    • J john_hobbyist

      So, the problem is that I have a GUI, the whole code is written on python and PyQt5. I have a button (BT1) where I press it and runs class_1, which exists on file_1. When the class_1 runs and stores a value in var_1 and then finishes. Let's say var_1 = 10. Then I click on another button (BT2) and then class_2 runs, which is stored in a different file (file_2). I need to pass the value from var_1 to var_2. The var_2 is a variable of class_2. I tried global variables but, I cannot pass the value from var_1 to var_2. Any idea how to achieve that? I have also tried many stackoverflow suggestions without any success.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @john_hobbyist
      This is basic C++ or Python. An instance of one class cannot see data in an instance of another class unless you pass one instance (or data from it) to the other instance, in some shape or form. Might be as a parameter to a method of the other class, might be via a setter method in the other class, might be via a signal/slot, ... Many ways, nobody can tell you from your vague description of what you want.

      J 1 Reply Last reply
      2
      • JonBJ JonB

        @john_hobbyist
        This is basic C++ or Python. An instance of one class cannot see data in an instance of another class unless you pass one instance (or data from it) to the other instance, in some shape or form. Might be as a parameter to a method of the other class, might be via a setter method in the other class, might be via a signal/slot, ... Many ways, nobody can tell you from your vague description of what you want.

        J Offline
        J Offline
        john_hobbyist
        wrote on last edited by
        #3

        @JonB said in How to pass a value from one class to another where each class is on different file:

        signal/slot

        So, If I use signal/slot, where the value sent from class_1 is stored until I press BT2, and then class_2 get the value? I think I try signal/slot to see what happens...

        JonBJ 1 Reply Last reply
        0
        • J john_hobbyist

          @JonB said in How to pass a value from one class to another where each class is on different file:

          signal/slot

          So, If I use signal/slot, where the value sent from class_1 is stored until I press BT2, and then class_2 get the value? I think I try signal/slot to see what happens...

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @john_hobbyist said in How to pass a value from one class to another where each class is on different file:

          where the value sent from class_1 is stored until I press BT2, and then class_2 get the value

          That is your problem to code! The answer is probably to store that value received from class_1 as a member variable in class_2. Then the instance of class_2 has it in its self.some_variable.

          J 1 Reply Last reply
          1
          • JonBJ JonB

            @john_hobbyist said in How to pass a value from one class to another where each class is on different file:

            where the value sent from class_1 is stored until I press BT2, and then class_2 get the value

            That is your problem to code! The answer is probably to store that value received from class_1 as a member variable in class_2. Then the instance of class_2 has it in its self.some_variable.

            J Offline
            J Offline
            john_hobbyist
            wrote on last edited by
            #5

            @JonB said in How to pass a value from one class to another where each class is on different file:

            @john_hobbyist said in How to pass a value from one class to another where each class is on different file:

            where the value sent from class_1 is stored until I press BT2, and then class_2 get the value

            That is your problem to code! The answer is probably to store that value received from class_1 as a member variable in class_2. Then the instance of class_2 has it in its self.some_variable.

            How I do that???

            JonBJ 1 Reply Last reply
            0
            • J john_hobbyist

              @JonB said in How to pass a value from one class to another where each class is on different file:

              @john_hobbyist said in How to pass a value from one class to another where each class is on different file:

              where the value sent from class_1 is stored until I press BT2, and then class_2 get the value

              That is your problem to code! The answer is probably to store that value received from class_1 as a member variable in class_2. Then the instance of class_2 has it in its self.some_variable.

              How I do that???

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @john_hobbyist
              This is so basic Python I don't know what to say. class_2_instance.some_variable = class_1_instance.a_variable ultimately, but depends where you put it, e.g. this example would be from somewhere which has access to class_2_instance and class_1_instance. Would be different from somewhere else (e.g. inside class_1 or class_2, but that is the principle. It's just member variables.

              J 1 Reply Last reply
              1
              • JonBJ JonB

                @john_hobbyist
                This is so basic Python I don't know what to say. class_2_instance.some_variable = class_1_instance.a_variable ultimately, but depends where you put it, e.g. this example would be from somewhere which has access to class_2_instance and class_1_instance. Would be different from somewhere else (e.g. inside class_1 or class_2, but that is the principle. It's just member variables.

                J Offline
                J Offline
                john_hobbyist
                wrote on last edited by john_hobbyist
                #7

                @JonB said in How to pass a value from one class to another where each class is on different file:

                @john_hobbyist
                This is so basic Python I don't know what to say. class_2_instance.some_variable = class_1_instance.a_variable ultimately, but depends where you put it, e.g. this example would be from somewhere which has access to class_2_instance and class_1_instance. Would be different from somewhere else (e.g. inside class_1 or class_2, but that is the principle. It's just member variables.

                Thank you! I think I have tried that, I mean you have to call the class somewhere in order to get the values, right? But what I face is not the common python code (it is PyQt5 python code)...because each class is called when I press each button...

                JonBJ 1 Reply Last reply
                0
                • J john_hobbyist

                  @JonB said in How to pass a value from one class to another where each class is on different file:

                  @john_hobbyist
                  This is so basic Python I don't know what to say. class_2_instance.some_variable = class_1_instance.a_variable ultimately, but depends where you put it, e.g. this example would be from somewhere which has access to class_2_instance and class_1_instance. Would be different from somewhere else (e.g. inside class_1 or class_2, but that is the principle. It's just member variables.

                  Thank you! I think I have tried that, I mean you have to call the class somewhere in order to get the values, right? But what I face is not the common python code (it is PyQt5 python code)...because each class is called when I press each button...

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @john_hobbyist
                  My friend, your understanding of "classes" and "instances" is all over the place...! ;-)

                  It's hard to help you because you do not explain where you create your instances of what and where you keep references around. It's not a PyQt5 issue, and yes I do understand both PyQt & Python :)

                  Somewhere you must create instances of your two classes. But I don't know where/how you do that....

                  Let's take a possible example related to your situation. Let's say your class_1 and class_2 are "windows" or "widgets" or "dialogs", e.g. derived from QWidget. And let's say you create the instances from a MainWindow class. Then you might have something like this:

                  # in `MainWindow` class, somewhere
                  self.window1 = class_1()
                  self.window2 = class_2()
                  
                  # connect the signal raised from `class_1` with its `var_1` parameter
                  # to call the slot in `class_2` with that value as a parameter
                  self.window1.button_clicked.connect(self.window2.on_window1_button_clicked)
                  
                  
                  # in `class_1` class, somewhere
                  # you must create your own "PyQt signal" for the ` self.button_clicked_signal` we are going to `emit` below
                  # you must look up the PyQt5 syntax for that, it is *something* like (as a class variable):
                  pyqtSignal(button_clicked_signal)
                  
                  # connect the Qt `QPushButton.clicked()` signal to `self.on_some_button_clicked()`
                  self.some_button.clicked.connect(self.on_some_button_clicked)
                  
                  # this is a *slot*
                  def on_some_button_clicked(self):
                      self.button_clicked_signal.emit(self.var_1)    # `button_clicked_signal` is the signal you created, we pass it `self.var_1`'s value
                  
                  
                  # in `class_2` class, somewhere
                  # this is a *slot*
                  def on_window1_button_clicked(self, var_1):
                      self.var_2 = var_1
                  

                  The above is one of many ways you can achieve what you want. [UPDATE I changed it a bit, and added some more comments.]

                  1 Reply Last reply
                  2
                  • J Offline
                    J Offline
                    john_hobbyist
                    wrote on last edited by john_hobbyist
                    #9

                    Why do I get this error?

                        Signal(button_clicked_signal)
                    NameError: name 'button_clicked_signal' is not defined
                    

                    From here:

                    # in `class_1` class, somewhere
                    # you must create your own "PyQt signal" for the ` self.button_clicked_signal` we are going to `emit` below
                    # you must look up the PyQt5 syntax for that, it is *something* like (as a class variable):
                    pyqtSignal(button_clicked_signal)
                    

                    I changed this:

                    pyqtSignal(button_clicked_signal)
                    

                    to this:

                     Signal(button_clicked_signal)
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi,

                      Because it's wrong.

                      The correct way to declare a signal is:

                      class MyWidget(QWidget):
                          mySignal = Signal()
                      

                      Between the parenthesis, put the type for the arguments if any.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Hi,

                        Because it's wrong.

                        The correct way to declare a signal is:

                        class MyWidget(QWidget):
                            mySignal = Signal()
                        

                        Between the parenthesis, put the type for the arguments if any.

                        J Offline
                        J Offline
                        john_hobbyist
                        wrote on last edited by john_hobbyist
                        #11

                        @SGaist said in How to pass a value from one class to another where each class is on different file:

                        Hi,

                        Because it's wrong.

                        The correct way to declare a signal is:

                        class MyWidget(QWidget):
                            mySignal = Signal()
                        

                        Between the parenthesis, put the type for the arguments if any.

                           self.window1.triggered.connect(self.window2.on_window1_button_clicked)
                        AttributeError: 'class_1' object has no attribute 'triggered'
                        

                        I use menus instead of buttons. And I click them. How to fix the above error?

                        JonBJ 1 Reply Last reply
                        0
                        • J john_hobbyist

                          @SGaist said in How to pass a value from one class to another where each class is on different file:

                          Hi,

                          Because it's wrong.

                          The correct way to declare a signal is:

                          class MyWidget(QWidget):
                              mySignal = Signal()
                          

                          Between the parenthesis, put the type for the arguments if any.

                             self.window1.triggered.connect(self.window2.on_window1_button_clicked)
                          AttributeError: 'class_1' object has no attribute 'triggered'
                          

                          I use menus instead of buttons. And I click them. How to fix the above error?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @john_hobbyist
                          self.window1 is of class_1. The error message tells you that has no method/member named triggered. Since you do not show what class_1, how should people answer "How to fix the above error? Either use a class which has a triggered member or do not try to access self.window1.triggered?

                          J 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @john_hobbyist
                            self.window1 is of class_1. The error message tells you that has no method/member named triggered. Since you do not show what class_1, how should people answer "How to fix the above error? Either use a class which has a triggered member or do not try to access self.window1.triggered?

                            J Offline
                            J Offline
                            john_hobbyist
                            wrote on last edited by
                            #13

                            @JonB said in How to pass a value from one class to another where each class is on different file:

                            @john_hobbyist
                            self.window1 is of class_1. The error message tells you that has no method/member named triggered. Since you do not show what class_1, how should people answer "How to fix the above error?? Either use a class which has a triggeredmember or do not try to accessself.window1.triggered`?

                            I copy paste from your previously posted code above:

                            # in `MainWindow` class, somewhere
                            self.window1 = class_1()
                            self.window2 = class_2()
                            
                            # connect the signal raised from `class_1` with its `var_1` parameter
                            # to call the slot in `class_2` with that value as a parameter
                            self.window1.button_clicked.connect(self.window2.on_window1_button_clicked)
                            
                            
                            # in `class_1` class, somewhere
                            # you must create your own "PyQt signal" for the ` self.button_clicked_signal` we are going to `emit` below
                            # you must look up the PyQt5 syntax for that, it is *something* like (as a class variable):
                            pyqtSignal(button_clicked_signal)
                            
                            # connect the Qt `QPushButton.clicked()` signal to `self.on_some_button_clicked()`
                            self.some_button.clicked.connect(self.on_some_button_clicked)
                            
                            # this is a *slot*
                            def on_some_button_clicked(self):
                                self.button_clicked_signal.emit(self.var_1)    # `button_clicked_signal` is the signal you created, we pass it `self.var_1`'s value
                            
                            
                            # in `class_2` class, somewhere
                            # this is a *slot*
                            def on_window1_button_clicked(self, var_1):
                                self.var_2 = var_1
                            

                            What should I change here? I implement the above rationale to my code. I cannot post my code is too huge for here, but I follow your logic...

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              There's no triggered signal anywhere in sight in your code.

                              What can you do ?

                              Create minimal script with a small UI where you can actually properly experiment and implement signals and slots rather than trying to cram it in your huge code base. Once you have understood how it working, then you will be able to translate that to your application.

                              On a side note, please give your classes and variable meaningful names. Random things like class_1 does not make your code readable nor easy to reason about.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J 1 Reply Last reply
                              1
                              • SGaistS SGaist

                                There's no triggered signal anywhere in sight in your code.

                                What can you do ?

                                Create minimal script with a small UI where you can actually properly experiment and implement signals and slots rather than trying to cram it in your huge code base. Once you have understood how it working, then you will be able to translate that to your application.

                                On a side note, please give your classes and variable meaningful names. Random things like class_1 does not make your code readable nor easy to reason about.

                                J Offline
                                J Offline
                                john_hobbyist
                                wrote on last edited by
                                #15

                                @SGaist said in How to pass a value from one class to another where each class is on different file:

                                There's no triggered signal anywhere in sight in your code.

                                What can you do ?

                                Create minimal script with a small UI where you can actually properly experiment and implement signals and slots rather than trying to cram it in your huge code base. Once you have understood how it working, then you will be able to translate that to your application.

                                On a side note, please give your classes and variable meaningful names. Random things like class_1 does not make your code readable nor easy to reason about.

                                I have spent many days studying tutorials such as this: https://programmer.group/pyqt5-quick-start-pyqt5-signal-slot-mechanism.html, this: https://www.geeksforgeeks.org/pyqt5-qaction/ and many stackoverflow questions.... What I mean is that I have ran code and see what happens. Do I need these lines from the code above:

                                # in `MainWindow` class, somewhere
                                self.window1 = class_1()
                                self.window2 = class_2()
                                

                                ???

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  That's quite a strange question to ask. If you want to use objects from either of class_1 or class_2 in your MainWindow class, then yes you need them.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  J 1 Reply Last reply
                                  1
                                  • SGaistS SGaist

                                    That's quite a strange question to ask. If you want to use objects from either of class_1 or class_2 in your MainWindow class, then yes you need them.

                                    J Offline
                                    J Offline
                                    john_hobbyist
                                    wrote on last edited by
                                    #17

                                    @SGaist said in How to pass a value from one class to another where each class is on different file:

                                    That's quite a strange question to ask. If you want to use objects from either of class_1 or class_2 in your MainWindow class, then yes you need them.

                                    So, since

                                    triggered.connect
                                    

                                    does not work on them, how I send the signal when the menus (that call those functions) are chosen? I am confused...

                                    JonBJ 1 Reply Last reply
                                    0
                                    • J john_hobbyist

                                      @SGaist said in How to pass a value from one class to another where each class is on different file:

                                      That's quite a strange question to ask. If you want to use objects from either of class_1 or class_2 in your MainWindow class, then yes you need them.

                                      So, since

                                      triggered.connect
                                      

                                      does not work on them, how I send the signal when the menus (that call those functions) are chosen? I am confused...

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @john_hobbyist said in How to pass a value from one class to another where each class is on different file:

                                      does not work on them, how I send the signal when the menus (that call those functions) are chosen? I am confused...

                                      Q: What Qt object has a triggered signal?
                                      A: void QAction::triggered(bool checked = false)

                                      This signal is emitted when an action is activated by the user; for example, when the user clicks a menu option, toolbar button, or presses an action's shortcut key combination, or when trigger() was called

                                      So you need QActions for your QMenu items:

                                      Normally, you connect each menu action's triggered() signal to its own custom slot

                                      1 Reply Last reply
                                      2
                                      • J Offline
                                        J Offline
                                        john_hobbyist
                                        wrote on last edited by john_hobbyist
                                        #19

                                        I left signals and slots. I am trying with getters and setters... like here: https://www.geeksforgeeks.org/getter-and-setter-in-python/

                                        I have stuck here... This is part of my code:

                                        class class_1():
                                        	def do_this(self):
                                        		...
                                        		# code here...
                                        		...
                                        		self.myvar = "Ok"
                                        		...
                                        	def get_var(self):
                                        		return self.myvar
                                        
                                        class class_2():
                                        	def do_something(self):
                                        		raj = class_1()
                                        		self.var = raj.get_var()
                                        		print("var = ", self.var) # here I check the value
                                        		...
                                        

                                        But I get this error:

                                            raj = class_1()
                                        UnboundLocalError: local variable 'class_1' referenced before assignment
                                        

                                        Any idea what I miss here?

                                        JonBJ 1 Reply Last reply
                                        0
                                        • J john_hobbyist

                                          I left signals and slots. I am trying with getters and setters... like here: https://www.geeksforgeeks.org/getter-and-setter-in-python/

                                          I have stuck here... This is part of my code:

                                          class class_1():
                                          	def do_this(self):
                                          		...
                                          		# code here...
                                          		...
                                          		self.myvar = "Ok"
                                          		...
                                          	def get_var(self):
                                          		return self.myvar
                                          
                                          class class_2():
                                          	def do_something(self):
                                          		raj = class_1()
                                          		self.var = raj.get_var()
                                          		print("var = ", self.var) # here I check the value
                                          		...
                                          

                                          But I get this error:

                                              raj = class_1()
                                          UnboundLocalError: local variable 'class_1' referenced before assignment
                                          

                                          Any idea what I miss here?

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by
                                          #20

                                          @john_hobbyist

                                          • Did you import the class1 module into the class_2 module?
                                          • Is your code indeed as you show?
                                          • Does it really say it fails on the raj = class_1() statement?
                                          • Are you aware, even if you get this working, that raj = class_1() will create a new instance of class_1? And that is really what you intend/need for your question, and you're not going to come back and say "but this way it doesn't access the existing class_2 instance/widget I had"... ?
                                          J 1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved