【问题标题】:passing pyqt object to swig exported c++ code将 pyqt 对象传递给 swig 导出的 c++ 代码
【发布时间】:2011-05-28 18:05:44
【问题描述】:

有人知道是否存在将 pyQt 对象传递给 c++ 代码的方法吗?

我会解释得更好;说我有这个 c++ 代码

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

QtGuiPDLVisitor 是使用 SWIG 在 python 中导出的 c++ 类,所以在 python 中我想做这样的事情

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

有可能吗? python解释器说不:(...有什么解决方法,比如传递指针或任何其他黑客的方法?

编辑:

Traceback (most recent call last):
  File "/home/picci/workspace/gestface/pypelab/src/pypelab/ui/pipeworkarea.py", line 53, in create_new_computation
    subWindowWidget.setComputation(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 *'

这是来自 python 的错误。我已经知道这个错误是由于 Qt4 是使用 SIP 的 python 包装器,而我使用 SWIG 来包装,但我认为必须有一种方法可以直接访问 Qlayout 对象的指针并传递从 python 到 C++ 代码。

【问题讨论】:

  • SWIG 应该使精确的 Python 代码工作。它会为你做指针转换。你说 Python 解释器给你一个错误,那你为什么不贴出来?

标签: c++ pyqt swig


【解决方案1】:

好的,我实际上找到了一种方法来做到这一点。我用过

visitor.set_layout_address(sip.unwrapinstance(layout))

来自python,而不是c++

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

并且工作了!

这里sip doc 了解更多关于 sip.unwrapinstance()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多