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
QtWS25 Last Chance

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.3k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 6 Jun 2022, 18:28 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 7 Jun 2022, 09:33
    1
    • S SGaist
      6 Jun 2022, 18:28

      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 7 Jun 2022, 09:33 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 7 Jun 2022, 09:36 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 7 Jun 2022, 09:43
        1
        • S SGaist
          7 Jun 2022, 09:36

          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 7 Jun 2022, 09:43 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...

          J 1 Reply Last reply 7 Jun 2022, 10:39
          0
          • J john_hobbyist
            7 Jun 2022, 09:43

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

            J Offline
            J Offline
            JonB
            wrote on 7 Jun 2022, 10:39 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 8 Jun 2022, 11:17 last edited by john_hobbyist 6 Aug 2022, 11:42
              #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?

              J 1 Reply Last reply 8 Jun 2022, 11:44
              0
              • J john_hobbyist
                8 Jun 2022, 11:17

                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?

                J Offline
                J Offline
                JonB
                wrote on 8 Jun 2022, 11:44 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 8 Jun 2022, 11:59
                1
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 8 Jun 2022, 11:57 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
                  • J JonB
                    8 Jun 2022, 11:44

                    @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 8 Jun 2022, 11:59 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 8 Jun 2022, 14:08 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 8 Jun 2022, 14:44
                      3
                      • S SGaist
                        8 Jun 2022, 14:08

                        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 8 Jun 2022, 14:44 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
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 8 Jun 2022, 15:03 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 8 Jun 2022, 19:22
                          1
                          • L Offline
                            L Offline
                            lanas
                            wrote on 8 Jun 2022, 15:20 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
                            
                            S 1 Reply Last reply 8 Jun 2022, 18:07
                            0
                            • L lanas
                              8 Jun 2022, 15:20

                              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
                              
                              S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 8 Jun 2022, 18:07 last edited by
                              #27

                              @lanas PyQt is a set of Python bindings for the Qt framework, it does not do special things outside of the Python realm. The same rules apply.

                              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
                              2
                              • S SGaist
                                8 Jun 2022, 15:03

                                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.

                                J Offline
                                J Offline
                                john_hobbyist
                                wrote on 8 Jun 2022, 19:22 last edited by john_hobbyist 6 Aug 2022, 19:23
                                #28

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

                                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.

                                Yes, I do! Nothing works! Apart from signals/slots and getters/setters how can I pass the value from class_1 to class_2? class_1 and class_2 are stored in different files...

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 8 Jun 2022, 19:32 last edited by
                                  #29

                                  Well, this is the standard way to do it.

                                  The fact that your classes are in two different files just means that you have to use proper imports to be able to use them. Nothing more.

                                  From the looks of it, you essentially need to clean up your code base.

                                  As suggested earlier, you should write a small dummy project that does what you want and then adapt that to your big code base.

                                  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 8 Jun 2022, 19:36
                                  2
                                  • S SGaist
                                    8 Jun 2022, 19:32

                                    Well, this is the standard way to do it.

                                    The fact that your classes are in two different files just means that you have to use proper imports to be able to use them. Nothing more.

                                    From the looks of it, you essentially need to clean up your code base.

                                    As suggested earlier, you should write a small dummy project that does what you want and then adapt that to your big code base.

                                    J Offline
                                    J Offline
                                    john_hobbyist
                                    wrote on 8 Jun 2022, 19:36 last edited by
                                    #30

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

                                    Well, this is the standard way to do it.

                                    The fact that your classes are in two different files just means that you have to use proper imports to be able to use them. Nothing more.

                                    From the looks of it, you essentially need to clean up your code base.

                                    As suggested earlier, you should write a small dummy project that does what you want and then adapt that to your big code base.

                                    I tried this: https://www.geeksforgeeks.org/getter-and-setter-in-python/ and this: https://programmer.group/pyqt5-quick-start-pyqt5-signal-slot-mechanism.html that have source code. I will try again

                                    1 Reply Last reply
                                    0
                                    • J Offline
                                      J Offline
                                      john_hobbyist
                                      wrote on 9 Jun 2022, 14:34 last edited by john_hobbyist 6 Sept 2022, 14:36
                                      #31

                                      @JonB : What's the purpose of these lines:

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

                                      ?

                                      J 1 Reply Last reply 9 Jun 2022, 16:15
                                      0
                                      • J john_hobbyist
                                        9 Jun 2022, 14:34

                                        @JonB : What's the purpose of these lines:

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

                                        ?

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 9 Jun 2022, 16:15 last edited by
                                        #32

                                        @john_hobbyist
                                        In the example, class_1 & class_2 are each some kind of widget/window classes, each derived from QWidget. class_1() & class_2() each create a new instance (window in this case) of the respective class. I store the references in two member variables of self, perhaps your MainWindow class.

                                        1 Reply Last reply
                                        1
                                        • J Offline
                                          J Offline
                                          john_hobbyist
                                          wrote on 9 Jun 2022, 18:15 last edited by
                                          #33

                                          I am working on this: https://doc.qt.io/qtforpython/examples/example_widgets__thread_signals.html which is perfect for what I want to achieve because it has int signals and includes 3 classes. I am trying to divide it on 3 files. Each class on a separate file and import the other 2 classes from the files. But I get the following error:

                                          (most likely due to a circular import)
                                          

                                          How would you divide it?

                                          1 Reply Last reply
                                          0

                                          23/48

                                          8 Jun 2022, 14:08

                                          • Login

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