Menu

PythonQt and Boost.Python

2010-12-02
2013-04-06
  • Maxim Chertov

    Maxim Chertov - 2010-12-02

    Hi! Thank you for your wonderful library!
    I was able to compile the application with Python 2.7, Qt 4.7 and Visual Studio 2008 (2010 does not compile). Everything works, but before I used Boost.Python. And I think it is very convenient library. I tried to connect PythonQt and objects that creates Boost.Python, but it's too hard …
    In Boost.Python I used simple types, functions and classes … std:: vector, etc. But I can not connect PySide and C++ Qt objects through Boost.Python. At the same time I write widgets in Python using PySide and add them to C++ QMainWindow using PythonQt.
    Please tell me whether it is possible to connect an interpreter PythonQt with objects Boost.Python … they are written on the basis of Python.h and use similar algorithms…
    What can i do?
    Thank you!


    Excuse my bad English …

     
  • Maxim Chertov

    Maxim Chertov - 2010-12-03

    Little progress! This simple code works)

    #include <stdlib.h>
    #include <iostream>
    #include <QApplication>
    #define BOOST_PYTHON_STATIC_LIB
    #include <boost/python.hpp>
    using namespace boost::python;
    #include <PythonQt.h>
    #include <PythonQt_QtAll.h>
    #include <gui/PythonQtScriptingConsole.h>
    char const* greet(int t)
    {
        if (t == 777) std::cout << t << std::endl;
        return "hello, world";
    }
    class CppClass {
    public:
        CppClass()
        {
            t = 0;
        }
        int getNum() {
            return t;
        }
        void inc()
        {
            t++;
        }
    private: 
        int t;
    };
    int main( int argc, char ** argv ) 
    {
        setlocale(LC_ALL, "Russian");
        QApplication qapp(argc, argv);
        PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
        PythonQt_QtAll::init();
        PythonQtObjectPtr  mainContext = PythonQt::self()->getMainModule();
        PythonQtScriptingConsole console(NULL, mainContext);
        console.show();
        try 
        {
            object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
            object main_namespace = main_module.attr("__dict__");
            main_namespace["greet"] = greet;
            main_namespace["CppClass"] = class_<CppClass>("CppClass")
                .def("getNum",&CppClass::getNum)
                .def("inc",&CppClass::inc);
            CppClass *cpp = new CppClass();
            main_namespace["cpp"] = ptr(cpp);
            PyRun_String( "print \"Python: \", cpp.getNum()\n", 
                Py_file_input, main_namespace.ptr(), main_namespace.ptr());
            std::cout << "C++ inc()" << std::endl;
            cpp->inc();
            PyRun_String( "print \"Python: \", cpp.getNum()\n"
                "cpp.inc()\n"
                "print \"Python inc()\" \n"
                "print \"Python: \", cpp.getNum()\n",
                Py_file_input, main_namespace.ptr(), main_namespace.ptr());
            std::cout << "C++: " << cpp->getNum() << std::endl;
            std::cout << "C++ inc()" << std::endl;
            cpp->inc();
            PyRun_String( "print \"Python: \", cpp.getNum()\n",
                Py_file_input, main_namespace.ptr(), main_namespace.ptr() );
        } 
        catch( error_already_set ) 
        {
            PyErr_Print();
        }
        PyRun_SimpleString("print greet(777)");
        int exit_code = qapp.exec();
        return exit_code;
    }
    
     

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.