[PyKDE] Re: Converting QStrings to Python strings

Jim Bublitz jbublitz at nwinternet.com
Mon May 6 18:39:01 BST 2002


On 06-May-02 Janos Blazi <jblazi at gmx.de> wrote:

> I thought that PyQt would convert QStrings to Python strings
> automatically, but this seems not to be true:

> ---------
> import sys
> from qt import *
> 
> app=QApplication(sys.argv)
> vbox=QVBox()
> 
> edit=QLineEdit(vbox)
> ok=QPushButton('Finish',vbox)
> QObject.connect(ok,SIGNAL("clicked()"),app,SLOT("quit()"))
> vbox.show()
> 
> app.exec_loop()
> print int(edit.text())
> ------------------------------------------------------------------

> In this code I read a string from a QLineEdit and try to convert
> it to an integer but this is not possible as the return value
> edit.text() is a QString. (I also get a segmentation fault, who
> knows, why?)

Probably because you're trying to convert a QString *instance* to an
int.
 
> So how can I convert the QString to a Python string?

   print int (str (edit.text ()))

If the Qt or KDE docs/h files indicate a method returns QString,
PyQt or PyKDE2 implement QString, *not* a Python string. str()
will convert the values to Python strings. If PyQt/PyKDE2 did the
conversion implicitly, there would be considerably more confusion
and limitations when passing methods calls as arguments or when
subclassing in addition to the fact that QString is more than a
simple C++ string wrapper.

Jim




More information about the PyQt mailing list