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 13.9k 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
    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
      • SGaistS SGaist

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

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

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

        Where is some_button located in your code ?
        answer: In main

        Where is on_some_button_clicked located in your code ?
        answer: In class_1

        1 Reply Last reply
        0
        • J Offline
          J Offline
          john_hobbyist
          wrote on last edited by john_hobbyist
          #44

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

          I need to pass the value from var_1 to var_2

          When I pass the var_1 (from class_1) to var_2 (from class_2) via the signals/slots, the beginning of class_1 and class_2 how should it be? Should I use init and things like this? I am searching it on literature...

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

            Did you check the Qt for Python documentation ?

            The connection is usually done by the class managing the objects you want to connect.

            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

              Did you check the Qt for Python documentation ?

              The connection is usually done by the class managing the objects you want to connect.

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

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

              Did you check the Qt for Python documentation ?

              The connection is usually done by the class managing the objects you want to connect.

              So you mean that I need to use this:

              @Slot(int)
              

              ??

              (source: https://doc.qt.io/qtforpython-6/tutorials/basictutorial/signals_and_slots.html )

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

                It's the documentation for PySide2/6 so there are some small differences with regard to PyQt.

                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
                  #48

                  Ok, can someone divide the code either this: https://doc.qt.io/qtforpython/examples/example_widgets__thread_signals.html or this: https://doc.qt.io/qtforpython-6/tutorials/basictutorial/signals_and_slots.html in 3 different files (for instance each file with one class) with all the related imports in order to understand the rationale?

                  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