Menu

PythonQtObjectPtr::evalScript() question

Help
johnw
2013-08-28
2013-08-28
  • johnw

    johnw - 2013-08-28

    Hi, I'm having trouble getting a value returned from evalScript(). I have the following C++ code:

    PythonQtObjectPtr _delegate;
    _delegate = APLab::instance()->aplabModule.evalScript("APLab.newDirectoryModelDelegate()");
    

    and Python code:

    def newDirectoryModelDelegate():
        from directory.directory_pane import DirectoryItemModelDelegate
        return DirectoryItemModelDelegate(directory)
    

    the returned PythonQtObjectPtr always has a 0 pointer in it. However, if I change the Python code to:

    def newDirectoryModelDelegate():
        global directoryModelDelegate
        from directory.directory_pane import DirectoryItemModelDelegate
        directoryModelDelegate = DirectoryItemModelDelegate(directory)
    

    and C++ to:

    APLab::instance()->aplabModule.evalScript("APLab.newDirectoryModelDelegate()");
    _delegate = APLab::instance()->aplabModule.getVariable("directoryModelDelegate");
    

    I get a good PythonQtObjectPtr. Shouldn't evalScript() return the value of the function call in the first case? I also tried capturing the function result in a module variable prior to returning the function result, in case of refCount issues, but same error.

    Any suggestions, I'd rather not use that thread-unsafe hack?

    Thanks,
    John.

     
  • Florian Link

    Florian Link - 2013-08-28

    You need to pass Py_eval_input as second parameter to evalScript(). Note that then you can only pass a single expression as source code.

    The reason is that Python code does not automatically return the last value as its result, only expressions do that.

    Alternatively, you can do:

    PythonQtObjectPtr _delegate;
    _delegate = APLab::instance()->aplabModule.call("APLab.newDirectoryModelDelegate");
    

    to call the method and get the result.

     
  • johnw

    johnw - 2013-08-28

    OK, thanks Florian, that fixed it!

     

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.