[PyKDE] Problem returning a QString

Jon jjhome at fire-bug.co.uk
Sun Jan 13 02:34:49 GMT 2002


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 

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.

Python 2.1.1


// JJTest .h

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

JJTest::JJTest(QObject *parent, const char *name) :
QObject(parent, name),

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

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

// JJTest.sip

class JJTest : QObject
    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()
        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

        a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))

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