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 14.1k 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.
  • 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
          • L lanas

            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 Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            • SGaistS SGaist

              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 last edited by john_hobbyist
              #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
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                2
                • SGaistS SGaist

                  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 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 last edited by john_hobbyist
                    #31

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

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

                    ?

                    JonBJ 1 Reply Last reply
                    0
                    • J john_hobbyist

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

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

                      ?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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 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
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #34

                          You are not even posting the error, how can we know ?

                          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
                          2
                          • SGaistS SGaist

                            You are not even posting the error, how can we know ?

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

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

                            You are not even posting the error, how can we know ?

                            from thread_signals_1 import MainForm
                            ImportError: cannot import name 'MainForm' from partially initialized module 'thread_signals_1' (most likely due to a circular import) (/home/someone/Desktop/test_code/PyQt5_signals_slots_test/thread_signals_1.py)
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #36

                              Please show the exact code you are currently using. All files.

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

                                Am I allowed to post code (changed though) from this link: https://doc.qt.io/qtforpython/examples/example_widgets__thread_signals.html here? I see something about copyright down in the above website.

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

                                  Post the code with the adequate copyright notice.

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

                                    I am working on JonB's code... After some modifications, I get this error:

                                    File "main_code.py", line 73, in __init__
                                      self.window1.oneParameterSignal.connect(test.onOneParameterSlot) 
                                    TypeError: class_1 cannot be converted to PyQt5.QtCore.QObject in this context
                                    

                                    I googled it, and I found this solution: https://stackoverflow.com/questions/36522809/pyqt5-connection-doesnt-work-item-cannot-be-converted-to-pyqt5-qtcore-qobject

                                    However I cannot understand what to do..even here it is not clear to me https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsObject.html#more

                                    (source of the modified code: https://programmer.group/pyqt5-quick-start-pyqt5-signal-slot-mechanism.html under the paragraph: 3. Advances in signal slot applications, 1. Custom Signal Slot).

                                    Any idea? :-)

                                    JonBJ 1 Reply Last reply
                                    0
                                    • J john_hobbyist

                                      I am working on JonB's code... After some modifications, I get this error:

                                      File "main_code.py", line 73, in __init__
                                        self.window1.oneParameterSignal.connect(test.onOneParameterSlot) 
                                      TypeError: class_1 cannot be converted to PyQt5.QtCore.QObject in this context
                                      

                                      I googled it, and I found this solution: https://stackoverflow.com/questions/36522809/pyqt5-connection-doesnt-work-item-cannot-be-converted-to-pyqt5-qtcore-qobject

                                      However I cannot understand what to do..even here it is not clear to me https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsObject.html#more

                                      (source of the modified code: https://programmer.group/pyqt5-quick-start-pyqt5-signal-slot-mechanism.html under the paragraph: 3. Advances in signal slot applications, 1. Custom Signal Slot).

                                      Any idea? :-)

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

                                      @john_hobbyist
                                      class_1 does not inherit from QObject so cannot emit signals, hence that error message I would say. I said I assumed your class_1 was supposed to be a widget, but goodness knows....

                                      You really need to grasp some of the basics, rather than just banging in code which does not make sense and seeing what happens. First you need to understand classes and instances, which is a C++ thing. Then for signals & slots have you read through https://doc.qt.io/qt-6/signalsandslots.html and understood it? Then read it again? If you follow what is there it has e.g.

                                      All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots

                                      J 1 Reply Last reply
                                      2
                                      • JonBJ JonB

                                        @john_hobbyist
                                        class_1 does not inherit from QObject so cannot emit signals, hence that error message I would say. I said I assumed your class_1 was supposed to be a widget, but goodness knows....

                                        You really need to grasp some of the basics, rather than just banging in code which does not make sense and seeing what happens. First you need to understand classes and instances, which is a C++ thing. Then for signals & slots have you read through https://doc.qt.io/qt-6/signalsandslots.html and understood it? Then read it again? If you follow what is there it has e.g.

                                        All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots

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

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

                                        @john_hobbyist
                                        class_1 does not inherit from QObject so cannot emit signals, hence that error message I would say. I said I assumed your class_1 was supposed to be a widget, but goodness knows....

                                        You really need to grasp some of the basics, rather than just banging in code which does not make sense and seeing what happens. First you need to understand classes and instances, which is a C++ thing. Then for signals & slots have you read through https://doc.qt.io/qt-6/signalsandslots.html and understood it? Then read it again? If you follow what is there it has e.g.

                                        All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots

                                        You are right!!!! I fixed it. I think I am close...

                                        This line:

                                        # connect the Qt `QPushButton.clicked()` signal to `self.on_some_button_clicked()`
                                        self.some_button.clicked.connect(self.on_some_button_clicked)
                                        

                                        either I put it in class_1 and when the class_1 is called, it should run. Either I put it in main class, where I have all the menu's choices connected to the related classes, where each class is a different file. What modifications should I do in the above command in any of the 2 cases?

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

                                          Where is some_button located in your code ?
                                          Where is on_some_button_clicked located in your code ?

                                          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

                                          • Login

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