[PyKDE] QStringList

Phil Thompson phil at river-bank.demon.co.uk
Wed Sep 27 19:34:02 BST 2000

Karolina Lindqvist wrote:
> I have started to get my old PyQt/PyKDE program to work with PyQt-2.0 and
> only Qt. Except for documented changed behaviour, I got the following problem:
> Code snippet:
>  print "filenames=", filenames, "count=", filenames.count()
>         for f in filenames:
>             print "f=", f
> Printout from execution:
> filenames= <qt.QStringList instance at 815dd50> count= 924
> Traceback (innermost last):
>   File "thumbs.py", line 558, in ?
>     appwidget.loadthumbs(argv[1])
>   File "thumbs.py", line 270, in loadthumbs
>     self.thumbs.loadthumbs(dirname)
>   File "thumbstable.py", line 109, in loadthumbs
>     for f in filenames:
> AttributeError: __getitem__
> So my question is, how do I access the individual items in a QStringList?
> It used to be with indexing [], but as you can see, that now gives Attribute
> error. The string list obviously contains 924 items, in the above example. I
> would also expect that printing out the QstringList would print out the list
> of items, or with str() or ``. I have some vague memory that it used to do
> that, but all three now gives the result <qt.QStringList instance at.... >
> The PyQt or Qt documentation does not appear to give any help.
> As this used to work, this appears to be a change of behaviour, but to what?

You have never been able to index QStringLists. My guess is that you are
using QDir.entryList() to get your file names. In Qt v1.x this returned
a QStrList which PyQt converts to a Python list under the covers which
you can index. In Qt v2.x it returns a QStringList which PyQt leaves as
a regular class. Unfortunately QStringList makes heavy use of operators
for accessing individual elements - which aren't supported by SIP. This
makes QStringList in PyQt almost useless.

Oddly enough I hit exactly the same problem this afternoon.

SIP supports the ability to specify that a class can be indexed - I've
just never got around to using it.  I'll add this in the next day or so
and mail the list when the CVS is updated. There will be a new release
of PyQt soon after October 10th (when Python v2.0 is released).

In the meantime the workaround is to use QDir.entryInfoList() to get a
list of QFileInfo instances (which you can index) and extract the
filename from there.


More information about the PyQt mailing list