[PyKDE] Printing empty QStrings

Boudewijn Rempt bsarempt at rempt.xs4all.nl
Tue Oct 31 22:09:55 GMT 2000


On Mon, 30 Oct 2000, Phil Thompson wrote:

> 
> It's the intended behaviour - but I'm not convinced it is the right
> behaviour.
> 

Well, let's try to find out which would be the more useful. The immediate
problem is that if I create a dialog box with a number of entry fields
(QLineEdits), I will have to check the result of text() with isEmpty(),
since trying to print it will give an exception. 

I'm not entirely clear on this, since I have been writing far too much
Java for my own good lately, but know I _can_ print Python None - perhaps
it would be enough to return None for QString(), and trigger no exception?

Thus:

 
>>> print (QString())
None
>>> print (QString(""))
 
>>> print (QString("a"))
a         

instead of:

>>> print (QString())
 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.0/site-packages/qt.py", line 898, in
__str__
    return str(self.sipThis)
TypeError: __str__ returned non-string (type None)
>>> print (QString(""))
 
>>> print (QString("a"))
a         

But well, I've been messing about with qt.py for a while, trying to
work out different schemes, and I'm not sure that the current situation
isn't the best... It seems quite difficult. I think the root is that a
QString and a Python string are almost, but not quite interchangeable.
There are situation when getting the text from a QLineEdit, that you
don't want a QString, but a Python string, and that should be None when
the QString is null, but "" when it's empty (or even u"" ;-)).

Maybe the best solution would be just to document the current behaviour
as regarding Python strings and QStrings...

> The problem is how to differentiate between a null QString and an empty
> QString. The choices are...
> 
> 1. Keep it as it is and have an empty QString return a zero length
> Python string and a null QString return None and trigger an exception.
> 
> 2. Return a zero length Python string for both empty and null strings
> (and use the appropriate QString methods if you really want to know if
> it is empty or null).
> 
> 3. Have a null QString return a Python string something like <null
> QString at ...>.
> 
> 4. Do (2) for str() and (3) for repr().
> 
> 5. Do something else.
> 
> Suggestions?
> 
> Phil
> 
> _______________________________________________
> PyKDE mailing list    PyKDE at mats.gmd.de
> http://mats.gmd.de/mailman/listinfo/pykde
> 






More information about the PyQt mailing list