2012-11-08 44 views
3

我用SIP制作一个C++库,以便在Python代码中使用它。Python,PyQt和C++库

我的问题是我没有设法将python QString对象传递给C++库函数。每个使用QString调用C++函数都会导致Python崩溃。 执行该功能时不会出现故障,但在开始该功能的第一步之前。 (所以我不把cpp文件在这里)

这里是我做的:

C++类:

#include "QString" 

Class CPythInteface 
{ 
public: 
    CPythInteface(); 

    // Trying a lot of prototypes:` 
    bool testSET(const QString* cSource); 
    bool testGET(QString* Source); 
    bool testGET2(QString& Source); 
    bool testGET3(char* zBuff); 
    bool testGET4(QString Source); 
    bool testSET4(QString Source); 
    QString getS1(const char* zVal); 
}; 

-

的SIP文件:

%Module libtest 1 
%Import QtCore/QtCoremod.sip 

class CPythInteface 
{ 

%TypeHeaderCode 
#include "CPythInteface.h" 
%End 

public: 

    CPythInteface(); 

    bool testSET(const QString*); 
    bool testGET(QString* ); 
    bool testGET2(QString& ); 
    bool testGET3(char* ); 
    bool testGET4(QString Source); 
    bool testSET4(QString Source); 

    QString getS1(const char* zVal); 
}; 

-

Python的用法:

>>> import libtest 
>>> obj = libtest. CPythInteface() 
>>> from PyQt4 import QtCore 
>>> qs1 = QtCore.QString("abc") 
>>> obj.testGET3("azertyuiop")  <-- OK 
>>> obj.testXXX(qs1)    <-- Crashes for all 
             functions SET/GET 

我做错了什么?它不是应该使用Python和C++的方式吗?

-

上面初始化的另一个测试:

>>> qs1 += obj.getS1("azert") 

给出了错误:

类型错误:不能连接 '的QString' 和 '的processError' 对象

这似乎以表明来自C++库的QString没有被Python正确理解。

回答

2

如果这会引起人们的兴趣,我发现问题是肯定的,因为PyQt提供了Qt 4.9.2(这不是公共版本),尽管我的C++库链接了Qt 4.6.3。