This instantiates the class properly, but my PythonQtObjectPtr "instancePtr" is null, and my QVariant "instance" variable is invalid. Has this behavior changed in since the thread I'm referring to was posted? Since I'm not getting a PythonQtObjectPtr back, I haven't been able to figure out a way to call any methods of this class instance.
probably you get a C++ pointer back, because your Python object inherits from a C++ class.
I think you should reconsider your design and not derive your Python objects from C++ objects.
I would create a Python base class and derive from that. If you want to access C++ parts of your application from it, I would create a Qobject that exposes my application Api and pass it to the constructor of the base class.
Deriving from C++ objects in Python only makes sense when having full wrappers with virtual method dispatch, like the ones generated for Qt (where you want to used derived classes at places where only C++ is known, e.g. deriving from Qwidget)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The instance I get back doesn't seem to be invalid anymore, but it seems like the type string I try to return points to invalid place in memory. I am also still getting a nullptr when I try to obtain my instancePtr variable. Is there something else I could be messing up?
Shows that my QVariant is of the type "PythonQtSafeObjectPtr". So this seems above board. And in fact, this pointer looks valid in the inspector before I leave the function that is calling the block. It seems like the contents of the instancePtr are being cleared when I leave the function scope, even though the instancePtr is an output of my method:
Hello again, I've been thinking about this, and I'm wondering if this object is disappearing because it's not actually getting assigned to an explicit variable with a name? Is there an elegant way of achieving this? I could try by calling PythonQt::self->mainModule()->evalScript(...), but I'm thinking there must be a better way than that.
One side problem that I've had was that PythonQt's parseFile() was failing on fflush sporadically. I have no idea why, but I replaced those routines with my own parser, and things are behaving nicely. Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello all,
I've been wrestling with this issue for a while, and I'm at my wit's end, so here we go. I'm trying to follow Florian's instructions to instantiate a class, as per this thread from many years ago: https://sourceforge.net/p/pythonqt/discussion/631393/thread/6a851191/. I would then like to call methods of this class.
To do this, I am trying to return an instance of the class as ssuch:
This instantiates the class properly, but my PythonQtObjectPtr "instancePtr" is null, and my QVariant "instance" variable is invalid. Has this behavior changed in since the thread I'm referring to was posted? Since I'm not getting a PythonQtObjectPtr back, I haven't been able to figure out a way to call any methods of this class instance.
For reference, here is my python script
I register the class as such:
And the class (with wrapper) looks like this:
Last edit: Flower lady 2019-11-19
probably you get a C++ pointer back, because your Python object inherits from a C++ class.
I think you should reconsider your design and not derive your Python objects from C++ objects.
I would create a Python base class and derive from that. If you want to access C++ parts of your application from it, I would create a Qobject that exposes my application Api and pass it to the constructor of the base class.
Deriving from C++ objects in Python only makes sense when having full wrappers with virtual method dispatch, like the ones generated for Qt (where you want to used derived classes at places where only C++ is known, e.g. deriving from Qwidget)
Hi Florian, thanks for the response. I tried what you said, and now my class is simply:
As before, I am trying to create a class instance via:
The instance I get back doesn't seem to be invalid anymore, but it seems like the type string I try to return points to invalid place in memory. I am also still getting a nullptr when I try to obtain my instancePtr variable. Is there something else I could be messing up?
Update, replacing my previous type check with:
Shows that my QVariant is of the type "PythonQtSafeObjectPtr". So this seems above board. And in fact, this pointer looks valid in the inspector before I leave the function that is calling the block. It seems like the contents of the instancePtr are being cleared when I leave the function scope, even though the instancePtr is an output of my method:
Is this some strangeness in the behavior of PythonQtObjectPtr objects? I wouldn't expect instancePtr to go out of scope here.
Last edit: Flower lady 2019-11-20
Hello again, I've been thinking about this, and I'm wondering if this object is disappearing because it's not actually getting assigned to an explicit variable with a name? Is there an elegant way of achieving this? I could try by calling PythonQt::self->mainModule()->evalScript(...), but I'm thinking there must be a better way than that.
Update:
Running:
QVariant instance = PythonQt::self()->getMainModule().evalScript("test=HelloWorld()");
Does not work either, I get an invalid QVariant returned.
Last edit: Flower lady 2019-11-20
Okay all, I got this working via:
One side problem that I've had was that PythonQt's
parseFile()
was failing on fflush sporadically. I have no idea why, but I replaced those routines with my own parser, and things are behaving nicely. Thanks!