我尝试从 Python 调用 C++ 函数,但我得到:
TypeError: in method 'drawColorWheel_NoPerf', argument 2 of type 'QPainter *'
我用的是swig2.0。
Python脚本: 小示例.py
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
import Qt4SWIGSimple
class MB_Window (QtGui.QWidget):
def __init__ (self, parent = None):
super (self.__class__, self).__init__ (parent)
def paintEvent (self, event):
painter = QtGui.QPainter (self)
LabelPos = QtCore.QPointF (100, 100)
painter.drawText (LabelPos, "%s" % ("Hello, world!"))
Radius = 2
Qt4SWIGSimple.drawColorWheel_NoPerf (Radius, painter)
# Traceback (most recent call last):
# File "./SmallExample.py", line 19, in paintEvent
# DrawColorWheel_NoPerf.drawColorWheel_NoPerf (Radius, painter)
# TypeError: in method 'drawColorWheel_NoPerf', argument 2 of type 'QPainter *'
def main (theArgs):
App = QtGui.QApplication (theArgs)
theMainWindow = MB_Window ()
theMainWindow.show ()
App.exec_ ()
if __name__ == "__main__":
main (sys.argv)
到目前为止我的界面:
Qt4SWIGSimple.i:
%include "typemaps.i"
%module Qt4SWIGSimple
%{
/*
void drawColorWheel_NoPerf (int radius, QPainter *painter);
*/
#include "Qt4SWIGSimple.h"
%}
/* Let's just grab the original header file here */
%include "Qt4SWIGSimple.h"
Qt4SWIGSimple.h:
//#ifndef MAINWINDOW_H
#include <QPainter>
//#endif // MAINWINDOW_H
#ifdef __cplusplus
extern "C" {
#endif
void drawColorWheel_NoPerf (int radius, QPainter *painter);
#ifdef __cplusplus
}
#endif
非常感谢您的帮助!
感谢https://stackoverflow.com/users/2001654/musicamante
还有一个问题
我找到了一个有效的解决方案。但我不太满意,因为我认为它应该更容易。
我将 python 脚本更改为:
...
import sip
PainterAddress = sip.unwrapinstance (painter)
DrawColorWheel_NoPerf.drawColorWheel_NoPerf_Sip (Radius, PainterAddress)
...
标题 Qt4SWIGSimple.h:至:
...
void drawColorWheel_NoPerf (int radius, QPainter *painter);
void drawColorWheel_NoPerf_Sip (int radius, long _painter_address);
...
并添加到c++文件中:
...
void drawColorWheel_NoPerf_Sip (int radius, long _painter_address) {
drawColorWheel_NoPerf (radius, reinterpret_cast<QPainter*> (_painter_address));
}
...