I have googling around and search forth and back in pythonqt forum and help but got no clues. :(
Why is it I cannot call my python function from Qt. Here's the code snippet:
the problem is that you do not pass "Py_eval_input" to the
PythonQtObjectPtr tag = mainContext.evalScript("PyClass()\n");
call. So the correct call is:
PythonQtObjectPtr tag = mainContext.evalScript("PyClass()", Py_eval_input);
This is because the Python interpreter only returns a result on script evaluation, if the input is passed as an expression using Py_eval_input, otherwise the return value will always be None.
Appart from that, a much more elegant way to call the constructor of your class is:
PythonQtObjectPtr tag = mainContext.call("PyClass", QVariantList());
You could even pass constructor args via the QVariantList.
And if you do that more often, you could even write it like this (and keep a reference to pyClass around):
PythonQtObjectPtr pyClass = mainContext.getVariable("PyClass");
PythonQtObjectPtr tag = pyClass.call(QVariantList())
regards,
Florian
P.S. You don't need to send me private mails, I get email notifications by the forum!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Florian, I just tried what you told me but still it doesn't work :(. Btw, I executed the example CPPPyWrapperExample.exe, but it did not show anything, no gui no error report on the console. However other example works but some of them are with error.
I did take a look at the source of CPPPyWrapperExample and yes it shows how to get reference of class in python, but in my windows it did not work, probably that's why my code doesn't work either. I am using win 7 x86 32 bit with vs2008 as the compiler.
Do you know what probably is going on? Thanks again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hm, can you please try the current SVN version? I did some fixes to the examples after the 2.0.1 release but never found the time to release that as 2.0.2.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes I did downloaded also from the latest svn (since I read in the forum that you had fixed the examples in the svn). I also compared it using folder to folder and file to file and they are identical (CPPPyWrapperExample). But still there's no output. Do you have any examples where I could call and execute a python class? What is the OS that you use?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is strange. We use PythonQt on VS 2005/2008/2010 and on Linux/MacOS and this works. I am quite busy right now, so I am afraid I have no time to look into this right now. Maybe you are experiencing a different problem, e.g. your could try loading your script from disk instead of as a resource. Maybe it is just the Qt resource system import that is not working?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have googling around and search forth and back in pythonqt forum and help but got no clues. :(
Why is it I cannot call my python function from Qt. Here's the code snippet:
And this is my python script:
Thank you for your help.
Note:
- If I wrote like below it works.
Sorry it should be like this..
Thanks
Hi,
the problem is that you do not pass "Py_eval_input" to the
PythonQtObjectPtr tag = mainContext.evalScript("PyClass()\n");
call. So the correct call is:
PythonQtObjectPtr tag = mainContext.evalScript("PyClass()", Py_eval_input);
This is because the Python interpreter only returns a result on script evaluation, if the input is passed as an expression using Py_eval_input, otherwise the return value will always be None.
Appart from that, a much more elegant way to call the constructor of your class is:
PythonQtObjectPtr tag = mainContext.call("PyClass", QVariantList());
You could even pass constructor args via the QVariantList.
And if you do that more often, you could even write it like this (and keep a reference to pyClass around):
PythonQtObjectPtr pyClass = mainContext.getVariable("PyClass");
PythonQtObjectPtr tag = pyClass.call(QVariantList())
regards,
Florian
P.S. You don't need to send me private mails, I get email notifications by the forum!
Hi Florian, I just tried what you told me but still it doesn't work :(. Btw, I executed the example CPPPyWrapperExample.exe, but it did not show anything, no gui no error report on the console. However other example works but some of them are with error.
I did take a look at the source of CPPPyWrapperExample and yes it shows how to get reference of class in python, but in my windows it did not work, probably that's why my code doesn't work either. I am using win 7 x86 32 bit with vs2008 as the compiler.
Do you know what probably is going on? Thanks again.
Hm, can you please try the current SVN version? I did some fixes to the examples after the 2.0.1 release but never found the time to release that as 2.0.2.
Hi,
Yes I did downloaded also from the latest svn (since I read in the forum that you had fixed the examples in the svn). I also compared it using folder to folder and file to file and they are identical (CPPPyWrapperExample). But still there's no output. Do you have any examples where I could call and execute a python class? What is the OS that you use?
This is strange. We use PythonQt on VS 2005/2008/2010 and on Linux/MacOS and this works. I am quite busy right now, so I am afraid I have no time to look into this right now. Maybe you are experiencing a different problem, e.g. your could try loading your script from disk instead of as a resource. Maybe it is just the Qt resource system import that is not working?
I see. Ok, I will try that one. But for now same as you I am also busy. Thanks for your help, I'll probably get back for updates.