thanks for this framework, I achieved quick success with implementing a python-scripting functionality in our C++/Qt-application.
But now I am struggeling with making lists of specific object types accessible in the scripting environment.
We have a Cpp-Class 'EInfo'. I made this accessible in Python through a new class EInfoPyWrapper and registerClass(), same as example "PyCPPWrapperExample". This works fine so far.
What I now want is a list of these object types as a return type of a function 'getAllEInfos()'. How can that be done? I tried it with another wrapper, but what is the correct return type of the getAllEInfos() function? QList of EInfo does not work, QList of EInfoPyWrapper does not work either. Same when I only store pointers (QList<EInfo*>)
Can someone help me and maybe give me example code how to do that properly? I am sure, this can be done...
Thanks in advance!
Alex
Last edit: Alexander Kosik 2014-05-27
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
And I don't see how to use QList in my python environment in general.
I would expect this to work, but it does not:
"from PythonQt.QtCore import *
value = QList<integer>()"
Error: name 'QList' is not defined
But I am using PythonQt_QtAll and therefore I think it should be included.
Any hints?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Note that PythonQt only supports a limited number of POD QList types and pointers, so QList<YourObject> will not work, but QList<YourObject*> will work if PythonQt knows YourObject.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you.
Not using QDir in Python environment and make use of pointers in return types is the crucial information for me.
I also needed to make a WrapperFactory for my EINfo class, but this works now.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Finally found an answer to my problem)
So basically it's impossible to pass/return QList with Custom types. But in case of pointers-to-type, is it actually safe/effective to return a list of Pointers on every call? As I unserstand, by default such objects' ownership is assumed to stay on C++ side. What happens, if the object is somehow modified in python? It should be modified directly, unless you implement some sort of Copy-On-Write in wrapper object... Another problem is possible "invalidation" of pointer on C++ side, while executing a python script. Say, you requested and object from C++, then called some function that possibly modified actual object in C++, so you "first" python object becomes invalid. So it's generally unsafe to return pointers to objects, residing in containers (QVector/QList)
Returning NEW objects' copies every time (passing ownership to python) seems not to be an option, when you make 1000's or 1M's successive python calls. I guess the only safe way here is to write some wrapper of C++ container class, allowing to get wrapped objects and then copy them to python list. Or write own conversions like in PythonQtConversion.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
thanks for this framework, I achieved quick success with implementing a python-scripting functionality in our C++/Qt-application.
But now I am struggeling with making lists of specific object types accessible in the scripting environment.
We have a Cpp-Class 'EInfo'. I made this accessible in Python through a new class EInfoPyWrapper and registerClass(), same as example "PyCPPWrapperExample". This works fine so far.
What I now want is a list of these object types as a return type of a function 'getAllEInfos()'. How can that be done? I tried it with another wrapper, but what is the correct return type of the getAllEInfos() function? QList of EInfo does not work, QList of EInfoPyWrapper does not work either. Same when I only store pointers (QList<EInfo*>)
Can someone help me and maybe give me example code how to do that properly? I am sure, this can be done...
Thanks in advance!
Alex
Last edit: Alexander Kosik 2014-05-27
QList<EInfo*> should work, PythonQt supports QList of pointers to known C++ objects.
Thanks for the fast answer.
And what is the correct return type?
Should a QList of EInfo* or EInfoPyWrapper* be returned?
And I don't see how to use QList in my python environment in general.
I would expect this to work, but it does not:
"from PythonQt.QtCore import *
value = QList<integer>()"
Error: name 'QList' is not defined
But I am using PythonQt_QtAll and therefore I think it should be included.
Any hints?
You need to use a Python list:
value = [1,2,3,4]
a.setValue(value)
where your slot looks like this:
void setValue(QList<int> list)
Note that PythonQt only supports a limited number of POD QList types and pointers, so QList<YourObject> will not work, but QList<YourObject*> will work if PythonQt knows YourObject.
Thank you.
Not using QDir in Python environment and make use of pointers in return types is the crucial information for me.
I also needed to make a WrapperFactory for my EINfo class, but this works now.
Finally found an answer to my problem)
So basically it's impossible to pass/return QList with Custom types. But in case of pointers-to-type, is it actually safe/effective to return a list of Pointers on every call? As I unserstand, by default such objects' ownership is assumed to stay on C++ side. What happens, if the object is somehow modified in python? It should be modified directly, unless you implement some sort of Copy-On-Write in wrapper object... Another problem is possible "invalidation" of pointer on C++ side, while executing a python script. Say, you requested and object from C++, then called some function that possibly modified actual object in C++, so you "first" python object becomes invalid. So it's generally unsafe to return pointers to objects, residing in containers (QVector/QList)
Returning NEW objects' copies every time (passing ownership to python) seems not to be an option, when you make 1000's or 1M's successive python calls. I guess the only safe way here is to write some wrapper of C++ container class, allowing to get wrapped objects and then copy them to python list. Or write own conversions like in PythonQtConversion.