Hi<br><br>I am developing PyQt app now, and I very often have crashes, connected with QString usage. There are 2 reasons for exceptions:<br>- QString contains unicode characters<br>- python method called for Qt string. Sometimes called somewhere deep in 3rdparty code, such as os.path.blabla(QString))<br>
See code examples at the end<br><br>It is easy to forget about QString conversions, because there are a few different cases:<br>- if you define string in Python code - it is Python string<br>- if you take it from PyQt4 - it is QString<br>
- If you passed Python string through signal-slot - it becomes QString. <br><br>The majority of methods may be called with either Python and Qt strings and work fine.<br>Some bugs are not reproduced, while you use ascii, but, are reproduced, when your user uses unicode<br>
Too difficult, too lot of problems<br><br>QString problem wad discussed there: <a href="http://thread.gmane.org/gmane.comp.python.pyqt-pykde/4037/">http://thread.gmane.org/gmane.comp.python.pyqt-pykde/4037/</a><br>Now, when PyQt4 is widely used, it maybe to late to drop QString, because some people use QString::endsWith(), and, probably, other compatibility problem way happen.<br>
<br>But, maybe it is possible to renew this discussion and create something like hybrid object, which is Python unicode string, but, has endsWith method (and others, if there are API differences), so may pretend to be a QString<br>
<br><br><br>Code examples:<br>Problem 1:<br><div style="margin-left: 40px;">In [1]: from PyQt4.QtCore import QString<br>In [2]: print QString('hello')<br>hello<br>In [3]: print QString('ะาษืลิ') # Cyrillic symbols here<br>
---------------------------------------------------------------------------<br>UnicodeEncodeError Traceback (most recent call last)<br>/home/a/code/mbox/core/src/lin/libonvif/<ipython console> in <module>()<br>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)<br></div>Problem 2:<br><div style="margin-left: 40px;">In [1]: from PyQt4.QtGui import QApplication, QFileDialog<br>
In [2]: a = QApplication([])<br>In [3]: path = QFileDialog.getOpenFileName()<br>In [4]: path<br>Out[4]: PyQt4.QtCore.QString(u'/home/a/code/mbox/core/src/lin/libonvif/Makefile')<br>In [5]: import os.path<br>In [6]: os.path.basename(path)<br>
---------------------------------------------------------------------------<br>AttributeError Traceback (most recent call last)<br>/home/a/code/mbox/core/src/lin/libonvif/<ipython console> in <module>()<br>
/usr/lib/python2.7/posixpath.pyc in basename(p)<br> 110 def basename(p):<br> 111 """Returns the final component of a pathname"""<br>--> 112 i = p.rfind('/') + 1<br> 113 return p[i:]<br>
114 <br>AttributeError: 'QString' object has no attribute 'rfind'<br></div><br>Best Regards,<br>Andrei Kopats,<br><a href="https://github.com/hlamer/mksv3">https://github.com/hlamer/mksv3</a><br>