Repository navigation
How to receive a return value from python correctly? #318
|
OS: Windows11 24H2 I do two tests about receiving return value from python. But if I change the TestPyObj Class to TestCppObj which is defined in Cpp code and registered to Python by PythonQt, the return value will be destroyed directly before the cpp code receive it. The Test Code is below The printed messages are: The class decleration is below: Thanks for any help. |
Replies: 1 comment
|
This is somewhat expected. Objects in Python are reference-counted. If the reference count goes down to zero, the object is deleted. In the case of Python objects, the reference count is increased by the PythonQtObjectPtr stored in the QVariant. For the C++ object, there is no extra reference count. After the C++ object is returned from the evalScript method, the scope of the Python code is deleted, the reference count of the wrapper goes back zero, and the wrapper and its wrapped object are deleted. There are two things you can do:
I guess the first option is easier though. |
This is somewhat expected. Objects in Python are reference-counted. If the reference count goes down to zero, the object is deleted.
In the case of Python objects, the reference count is increased by the PythonQtObjectPtr stored in the QVariant.
For the C++ object, there is no extra reference count. After the C++ object is returned from the evalScript method, the scope of the Python code is deleted, the reference count of the wrapper goes back zero, and the wrapper and its wrapped object are deleted.
There are two things you can do: