[PyQt] Comparing QString with QChar

Mark Summerfield mark at qtrac.eu
Fri Jun 27 13:09:46 BST 2008


On 2008-06-27, Hans-Peter Jansen wrote:
> Am Freitag, 27. Juni 2008 schrieb Mark Summerfield:
> > Hi,
> >
> > I got caught by this today:
> >     >>> from PyQt4.QtCore import *
> >     >>> s = QString("X")
> >     >>> c = QChar("X")
> >     >>> s[0] == c
> >
> >     False
> >
> >     >>> s[0] == QString(c)
> >
> >     True
> >
> > In C++/Qt if you do the s[0] == c comparison it will return true because
> > operator[] returns a const QChar &, but in PyQt4 s[0] is still a
> > QString---which makes sense because in Python a char is just a str of
> > length 1, but maybe this particular case could be handled more
> > intuitively?
>
> Mark, while I won't anticipate Phil's statement, the problem is: PyQt
> is unfortunately full of those unintuitivnesses, because C++ implicit
> casts are hard to handle here.
>
> If you start formulate the rules, for which you want to do automatic
> conversions, you will find yourself in a twisted maze quickly. Doing
> them one by one (as this case might imply) is an awful lot of work..
>
> The question is - where to start, where to stop?

I think that strings are the most important because they are used in all
contexts by all applications... but hopefully QString (and QVariant)
will disappear in the Python 3 version of PyQt where we'll just enjoy
str and object, plus a few static methods like
    QString.simplified(s:str)->str 

:-)

-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu



More information about the PyQt mailing list