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 Updated to NodeBB v4.3 + New Features

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.7k Views 1 Watching
  • 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.
  • 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
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #21

                                Beside the points of @JonB, please write a minimal complete script that we can take a look at. You only post small snipets from here and there then some error that we can only vaguely guess why they happen.

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

                                1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @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 Offline
                                  J Offline
                                  john_hobbyist
                                  wrote on last edited by
                                  #22

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

                                  @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"... ?

                                  Thank you for your comment! No, my code is very huge. I depict only the needed parts here... Do you have any better idea of how to pass the value from class_1() to class_2()? I tried signals/slots but I didn't achieve something. Any idea of how to pass the value with getters/setters?

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

                                    You realize to you linked to a blog post describing exactly how to implement a getter and a setter and how to use 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
                                    3
                                    • SGaistS SGaist

                                      You realize to you linked to a blog post describing exactly how to implement a getter and a setter and how to use them ?

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

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

                                      You realize to you linked to a blog post describing exactly how to implement a getter and a setter and how to use them ?

                                      I tried to follow the link but as you see above I face problems with my code...

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

                                        As already written earlier, the fact that you post snipets of parts of your code distributed makes it almost impossible to help you.

                                        As @JonB already suggested: you likely did not import class_1 properly in the file where you define class_2.

                                        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
                                        • L Offline
                                          L Offline
                                          lanas
                                          wrote on last edited by
                                          #26

                                          i do not know if there is a predefined method in PyQt but in python you can get the value from a function and call it in other class
                                          this is a simple prototype may helps you

                                          def idfunc(self):
                                                 id =self.user_idtext()
                                               
                                                 return id
                                          
                                          SGaistS 1 Reply Last reply
                                          0

                                          • Login

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