iksess
iksess

Reputation: 576

Python, PyQt and C++ library

I make a c++ library with SIP to use it in Python code.

My problem is I don't manage to pass a python QString object to the C++ library functions. Every call to a C++ function using a QString make Python crash. The crash doesn't appear while executing the function, but before starting the first step of the function. (So I don't put the cpp file here)

Here is what I do:

The C++ class:

#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 );
};

-

The sip file:

%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 ); 
};

-

The Python usage:

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

.

Am I doing something wrong ? Is it not the way Python and C++ should be used ?

-

Another test with above initialization :

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

gives the error:

TypeError: cannot concatenate 'QString' and 'ProcessError' objects

This seems to show that the QString from the C++ library is not correctly understood by the Python.

Upvotes: 3

Views: 950

Answers (1)

iksess
iksess

Reputation: 576

In case this would interest someone, I found the problem was definitely because PyQt was provided with Qt 4.9.2 (that is not a public version) although my C++ library linked Qt 4.6.3.

Upvotes: 2

Related Questions