How to pass a value from one class to another where each class is on different file
-
@JonB : What's the purpose of these lines:
# in `MainWindow` class, somewhere self.window1 = class_1() self.window2 = class_2()
?
@john_hobbyist
In the example,class_1
&class_2
are each some kind of widget/window classes, each derived fromQWidget
.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 ofself
, perhaps yourMainWindow
class. -
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?
-
You are not even posting the error, how can we know ?
-
@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)
-
Please show the exact code you are currently using. All files.
-
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.
-
Post the code with the adequate copyright notice.
-
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? :-)
-
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? :-)
@john_hobbyist
class_1
does not inherit fromQObject
so cannot emit signals, hence that error message I would say. I said I assumed yourclass_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
-
@john_hobbyist
class_1
does not inherit fromQObject
so cannot emit signals, hence that error message I would say. I said I assumed yourclass_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
@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 fromQObject
so cannot emit signals, hence that error message I would say. I said I assumed yourclass_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?
-
Where is some_button located in your code ?
Where is on_some_button_clicked located in your code ? -
Where is some_button located in your code ?
Where is on_some_button_clicked located in your code ?@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 mainWhere is on_some_button_clicked located in your code ?
answer: In class_1 -
@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...
-
Did you check the Qt for Python documentation ?
The connection is usually done by the class managing the objects you want to connect.
-
Did you check the Qt for Python documentation ?
The connection is usually done by the class managing the objects you want to connect.
@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 )
-
It's the documentation for PySide2/6 so there are some small differences with regard to PyQt.
-
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?