[PyQt] Just for fun: QCompleter accessing a QListWidget's internal model through a "proxy" ?

Matt Newell newellm at blur.com
Tue Aug 17 20:25:46 BST 2010


On Tuesday 17 August 2010 11:23:55 fpp wrote:
> Hi everyone,
> 
> After many moons of lurking I have finally subscribed to ask for
> advice about my current challenge:
> 
> In this app I'm working on (my first "real", non-trivial one,
> admittedly), I have a number of QListWidgets in the main window,
> holding plain strings, each with an associated QLineEdit for input.
> They work just fine, and there really is no need for anything more
> elaborate than list widgets for what they do.
> 
> Only now that it's almost finished, with everything in place and the
> code working well, I just discovered the existence of QCompleters by
> accident... and obviously now I want them in the line edits.
> 
> However, QCompleters are designed to work only with "real" models, not
> convenience widgets. Of course it's very easy to use them with a plain
> QStringList, but it feels somewhat clunky and inefficient to duplicate
> the list widget's content to the completer's string list, refreshing
> it each time that content changes... and I don't really feel like
> retooling the entire UI to use QListViews just for the sake of
> completers.
> 
> So I thought, perhaps naively, that it might be possible to build a
> kind of "proxy" model, that would feed a completer with data pulled
> from a list widget's hidden, internal model.
> 
I could be overlooking something but I don't see why you can't use the list 
widget's model directly.  The details of the implemenation may be private, but 
the QAbstractItemModel interface is still public and usable.

Should be as simple as
completer = QCompleter( listWidget.model(), parent )

Matt


More information about the PyQt mailing list