2009-09-17 5 views
3

PyQt와 Boost.Python 사이에서 위젯을 공유 할 수 있는지 궁금합니다.PyQT와 Boost.Python 사이의 위젯 공유

Qt를 사용하는 광산 응용 프로그램에 파이썬 인터프리터를 임베드 할 예정입니다. 내 응용 프로그램 사용자가 자신의 UI 위젯을 C++로 프로그래밍되고 Boost.Python을 통해 노출 된 UI 위젯에 임베드 할 수 있기를 바랍니다.

이것이 가능하며 어떻게해야합니까?

답변

2

일부 프록시 작성을 시도했지만 완전히 성공하지 못했습니다. 여기에이 문제를 해결하기위한 시도가 있지만 dir()은 작동하지 않습니다. 함수를 호출하는 것은 다소 효과적입니다.

원래의 boost.python 개체에 일치하는 특성이없는 경우 SIP에 래핑 된 추가 파이썬 개체를 만들어 모든 호출/특성을 해당 개체로 전달하는 것이 아이디어였습니다.

이 작업을 제대로 수행하기에는 파이썬 전문가가 부족합니다. :(

. (PPL 편집하고 여기에 업데이트 할 수 있도록이 코드는 단지 상용구를 반 구운대로 내가, 위키에이를 돌리겠다)

C++ :

#include "stdafx.h"  
#include <QtCore/QTimer> 

class MyWidget : public QTimer 
{ 
public: 
    MyWidget() {} 
    void foo() { std::cout << "yar\n"; } 
    unsigned long myself() { return reinterpret_cast<unsigned long>(this); } 
}; 

#ifdef _DEBUG 
BOOST_PYTHON_MODULE(PyQtBoostPythonD) 
#else 
BOOST_PYTHON_MODULE(PyQtBoostPython) 
#endif 
{ 
    using namespace boost::python; 

    class_<MyWidget, bases<>, MyWidget*, boost::noncopyable>("MyWidget"). 
     def("foo", &MyWidget::foo). 
     def("myself", &MyWidget::myself); 
} 

파이썬 :

from PyQt4.Qt import * 
import sys 

import sip 
from PyQtBoostPythonD import * # the module compiled from cpp file above 

a = QApplication(sys.argv) 
w = QWidget() 
f = MyWidget() 

def _q_getattr(self, attr): 
    if type(self) == type(type(MyWidget)): 
    raise AttributeError 
    else: 
    print "get %s" % attr 
    value = getattr(sip.wrapinstance(self.myself(), QObject), attr) 
    print "get2 %s returned %s" % (attr, value) 
    return value 

MyWidget.__getattr__ = _q_getattr 

def _q_dir(self): 
    r = self.__dict__ 
    r.update(self.__class__.__dict__) 
    wrap = sip.wrapinstance(self.myself(), QObject) 
    r.update(wrap.__dict__) 
    r.update(wrap.__class__.__dict__) 
    return r 

MyWidget.__dir__ = _q_dir 

f.start() 
f.foo() 
print dir(f)