开发者

passing pyqt object to swig exported c++ code

anyone knows if there exists a method for passing pyQt objects to c++ code?

I'll explain better; say i have this c++ code

    void QtGuiPDLVisitor::set_layout(QLayout* _layout){
       layout = _layout;
    }

where QtGuiPDLVisitor is a c++ class exported in python using SWIG, so in python i want to do something like this

    layout = QtGui.QGridLayout()
    visitor = QtGuiPDLVisitor()
    visitor.set_layout(layout)

is it possible? The python interpreter says no :(... is there any workaround, like a way of passing pointers or any other hack?

EDIT:

Traceback (most recent call last):
  File "/home/picci/workspace/gestface/pypelab/src/pypelab/ui/pipeworkarea.py", line 53, in create_new_computation
    subWindowWidget.s开发者_开发百科etComputation(idx.data())
  File "/home/picci/workspace/gestface/pypelab/src/pypelab/ui/pypeWidgets.py", line 44, in setComputation
    self.widBuilder.buildUi(self)
  File "/home/picci/workspace/gestface/pypelab/src/pypelab/ui/pypeWidgets.py", line 63, in buildUi
    pdlVisitor.set_layout(criteria_area_layout)
  File "/home/picci/workspace/gestface/pipeit/wrap/python/dist/Debug/pipeit.py", line 736, in set_layout
    def set_layout(self, *args) -> "void" : return _pipeit.QtGuiPDLVisitor_set_layout(self, *args)
TypeError: in method 'QtGuiPDLVisitor_set_layout', argument 2 of type 'QLayout *'

This is the error from python. I already know that this error is due to the fact that Qt4 is wrapper in python using SIP, while i'm using SWIG to wrap, but i think that there must be a way to access directly to the pointer of the Qlayout object and pass it to C++ code from python.


Ok i actually find a way to do that. I used

visitor.set_layout_address(sip.unwrapinstance(layout))

from python, and than in c++

void QtGuiPDLVisitor::set_layout_address(long _layout_address){
    layout = reinterpret_cast<QLayout*>(_layout_address);
}

and worked!

here sip doc for more about sip.unwrapinstance()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜