[PyKDE] Problem returning a QString

Jon jon at jjhome.fire-bug.co.uk
Sun Jan 13 15:47:05 GMT 2002


Hello

I am having a problem with the code shown below. I wrap the JJTest C++ class 
with no SIP build errors. In Python I call str1() still no errors. But when I 
try and print with str() I get the error shown.  The Python documentation 
says this error occurs because there are unicode chars in the sting returned 
by str1() - but there isnt and if I change str() to unicode() I get the same 
error.

I compared my str1() function with PyQt's QButton.caption() function and the 
C++ and SIP file layout is exactly the same. If I replace str1() with 
QButton.caption() the error goes away - I dont see what Im doing wrong.

PyQt-3.0
sip-3.0
Python 2.1.1

Jon

//////////////////////////////////////////////////////////////////////////////////////////////
// JJTest .h

class JJTest : public QObject
{ Q_OBJECT
public:
    JJTest(QObject *parent=0, const char *name=0);
    QString str1() const;
    char * str2() const;
private:
    QString theStr;
};

JJTest::JJTest(QObject *parent, const char *name) :
QObject(parent, name),
theStr("theString")
{
}

inline QString JJTest::str1() const
{
    return theStr;
}

inline char * JJTest::str2() const
{
  static char * p = "buf";
  return p;
}

//////////////////////////////////////////////////////////////////////////////////////////////
// JJTest.sip

class JJTest : QObject
{
public:
    JJTest(QObject * /TransferThis/ =0, const char * =0);
    QString str1() const;
    char * str2() const;
};

//////////////////////////////////////////////////////////////////////////////////////////////
// testArcDocView.py.py

if __name__ == "__main__":
	a = QApplication(sys.argv)

	mw = QMainWindow()
	mw.setCaption("Test");
	
	t = JJTest(None, "JJTest")
	aaa = t.str1()
	print str(aaa)		# ERROR HERE

	b = QButton(mw)
	b.setCaption("The Caption")
	bbb = b.caption()
	print str(bbb)		# NO ERROR HERE

	mw.setCentralWidget(b)
	mw.show()
	a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))
	a.exec_loop()
//////////////////////////////////////////////////////////////////////////////////////////////
// ERROR

Loading required GL library /usr/X11R6/lib/libGL.so.1.2
Traceback (most recent call last):
  File "/home/john/src/Komodo-1.2.6-33282/Komodo/callkomodo/kdb.py", line 
397, in _do_start
    self.kdb.run(code_ob, locals, locals)
  File "/usr/lib/python2.1/bdb.py", line 351, in run
    exec cmd in globals, locals
  File "/home/john/projectspy/arcview/testArcDocView.py", line 31, in ?
    print str(aaa)
UnicodeError: ASCII encoding error: ordinal not in range(128)
Process terminated with exit code 0





More information about the PyQt mailing list