将 pyqt 对象传递给 swig 导出的 c++代码

发布于 2024-11-09 13:53:33 字数 1371 浏览 3 评论 0原文

有人知道是否存在将 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是包装器的事实在python中使用SIP,虽然我使用SWIG来包装,但我认为必须有一种方法可以直接访问Qlayout对象的指针并将其从python传递给C++代码。

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.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 *'

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-11-16 13:53:33

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

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() 的更多信息

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文