Menu

Cannot execute python function from Qt

Help
2011-06-21
2013-04-06
  • Bondhan Novandy

    Bondhan Novandy - 2011-06-21

    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:

    PythonQt::init();
        PythonQt_QtAll::init();
        PythonQtObjectPtr  mainContext = PythonQt::self()->getMainModule();
        // add a QObject to the namespace of the main python context
        QTextBrowser *example = new QTextBrowser();
        example->setText("Hello World");
        QMainWindow::setCentralWidget(example);
        mainContext.addObject("example", example);
        mainContext.evalFile(":pyclass.py");
        PythonQtObjectPtr tag = mainContext.evalScript("PyClass()\n");
        tag.call("setName", QVariantList() << "Anonymous");
        QVariant name = tag.call("getName", QVariantList());
        mainContext.call("appendTxt", QVariantList() << "Appended Text");
        example->append(name.toString());
    

    And this is my python script:

    class PyClass:
            name = 'kosong'
            def getName(self):
                    return self.name
    
        def setName(self, newName):
                    self.name = newName
    
            def showGui(self):
                    example.append(self.name)
    def appendTxt(txt):
            example.append(txt)
    

    Thank you for your help.

    Note:
    - If I wrote like below it works.

    mainContext.evalFile("def appendTxt(txt):\n  example.append(txt)");
    mainContext.call("appendText", QVariantList());
    
     
  • Bondhan Novandy

    Bondhan Novandy - 2011-06-21

    Sorry it should be like this..

    mainContext.evalFile("def appendTxt(txt):\n  example.append(txt)");
    mainContext.call("appendText", QVariantList() << "Some String");
    

    Thanks

     
  • Florian Link

    Florian Link - 2011-06-21

    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!

     
  • Bondhan Novandy

    Bondhan Novandy - 2011-06-22

    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.

    C:\PythonQt2.0.1\lib>dir
     Volume in drive C has no label.
     Volume Serial Number is 1825-454B
     Directory of C:\PythonQt2.0.1\lib
    06/22/2011  08:40    <DIR>          .
    06/22/2011  08:40    <DIR>          ..
    06/22/2011  08:40            26,624 AccessClassPy.exe
    06/19/2011  13:26            14,848 CPPPyWrapperExample.exe
    06/19/2011  13:26            15,360 PyCPPWrapperExample.exe
    06/19/2011  13:26            13,312 PyCustomMetaTypeExample.exe
    06/19/2011  13:26            17,408 PyDecoratorsExample.exe
    06/19/2011  13:26            26,112 PyGettingStarted.exe
    06/19/2011  13:26            10,752 PyGuiExample.exe
    06/19/2011  13:26            13,312 PyLauncher.exe
    06/19/2011  13:26            23,040 PyScriptingConsole.exe
    06/19/2011  13:23         1,001,472 PythonQt.dll
    06/19/2011  13:23            85,505 PythonQt.exp
    06/19/2011  13:23           140,016 PythonQt.lib
    06/19/2011  13:26         5,200,896 PythonQt_QtAll.dll
    06/19/2011  13:24               682 PythonQt_QtAll.exp
    06/19/2011  13:24             1,954 PythonQt_QtAll.lib
    06/18/2011  21:28         6,352,896 QtGui4.dll
    06/18/2011  21:18           637,952 QtNetwork4.dll
    06/18/2011  21:30           593,920 QtOpenGL4.dll
    06/18/2011  21:18           146,432 QtSql4.dll
    06/18/2011  21:34           229,376 QtSvg4.dll
    06/18/2011  22:44         8,149,504 QtWebKit4.dll
    06/18/2011  21:17           259,072 QtXml4.dll
    06/12/2011  20:23            15,360 testPythonQt.exe
                  23 File(s)     22,975,805 bytes
                   2 Dir(s)  85,774,127,104 bytes free
    C:\PythonQt2.0.1\lib>CPPPyWrapperExample.exe
    C:\PythonQt2.0.1\lib>
    

    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.

     
  • Florian Link

    Florian Link - 2011-06-22

    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.

     
  • Bondhan Novandy

    Bondhan Novandy - 2011-06-22

    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?

     
  • Florian Link

    Florian Link - 2011-06-22

    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?

     
  • Bondhan Novandy

    Bondhan Novandy - 2011-06-22

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.