[英]Python, PyQt and C++ library
我使用SIP創建一個c ++庫,以便在Python代碼中使用它。
我的問題是我沒有設法將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" )
給出錯誤:
TypeError:無法連接'QString'和'ProcessError'對象
這似乎表明Python沒有正確理解C ++庫中的QString。
如果這會讓某人感興趣,我發現問題肯定是因為PyQt提供了Qt 4.9.2(這不是公共版本),盡管我的C ++庫鏈接了Qt 4.6.3。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.