ctypes 和 PySide
我正在使用 PySide 构建一个应用程序,需要完成一些图像处理,并且使用 Python 代码太慢了。因此,我编写了一个 .dll 文件来帮我完成这件事。函数定义如下:
extern "C" {
QRectF get_image_slant(QImage *img, float slantangle, float offset) {
现在我可以通过 ctypes 加载这个函数。但我似乎无法让 ctypes 接受 QImage。我尝试这样称呼它:
ext.get_image_slant(QImage(), 0, 0)
我得到的答复是:
File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
我厌倦了将 QImage 转换为 c_void_p 并且它也不喜欢那样。据我所知,python 中的 QImage() 应该精确映射到 C 中的 QImage * ,但 Python 似乎不理解这一点..
有什么方法可以强制转换吗?
I'm building an app with PySide, there's some image manipulation that needs to be done and using Python code for this is way too slow. Therefore I hacked out a .dll file that will do it for me. The function definition is as follows:
extern "C" {
QRectF get_image_slant(QImage *img, float slantangle, float offset) {
Now I can load this function in via ctypes. But I can't seem to get ctypes to accept a QImage. I tried calling it like this:
ext.get_image_slant(QImage(), 0, 0)
And the reply I get is:
File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
I tired casting the QImage to a c_void_p and it doesn't like that either. From what I can tell QImage() in python should map exactly to a QImage * in C, but Python doesn't seem to understand that..
Is there any way to force the casting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 C++ 的
ctypes
效果不太好,我建议使用 PySide 自己的包装器 - Shiboken。他们实际上用它来包装 Qt 库本身。由于您的代码处理 Qt 对象,这似乎是您的完美选择。Since
ctypes
for C++ isn't working very well, I would recommend using PySide's own wrapper - Shiboken. They actually use it to wrap the Qt libs themselves. Since your code deals with Qt object, this seems like the perfect choice for you.