[PySide] Displaying a list of Python objects inside a QML ListView

905 views
Skip to first unread message

Thomas Perl

unread,
Nov 20, 2010, 11:30:19 AM11/20/10
to pys...@lists.openbossa.org
Hi!

I'm trying to display a list of arbitrary Python objects inside a QML
ListView where I can leave the representation of each Python object to
the delegate. Ideally (ignoring all wrapping in QObjects with slots,
properties, etc..), I'd like to do something along those lines:

class Person:
def __init__(self, id, name):
self.id = id
self.name = name

# Imagine this is properly wrapped as slot in a QObject
def person_selected(p):
print 'user clicked on person:', p.name, 'with id:', p.id

persons = [
Person(1, 'AA'),
Person(2, 'BB'),
Person(3, 'CC')
]

# ... create QDeclarativeView ...
# ... get root context as "ctx" ...

ctx.setContextProperty('persons', persons)
ctx.setContextProperty('person_selected', person_selected)

In my QML file, I then want to set "persons" as the model for my
ListView, and set up the mouse handler in the delegate to call
person_selected() with the person that has been clicked. In the
delegate, I want to access the attributes of Person (e.g. p.name), and
(thinking about future uses), I might also use methods of it (e.g.
p.formatBirthday()) as well, not just attributes.

In the pyside-examples repository, I've only found a simple example
that deals with a list of strings (in
examples/declarative/scrolling.py), which works and is nice, but does
not really map to my real-world use case where I want to display a
list of items that have different properties and where I also want to
retrieve the original item in a "item selected" callback.

Trying to come up with something as described above does not give me
any results. I have tried playing around with QAbstractListModel as
alternative already, which resulted in
http://bugs.openbossa.org/show_bug.cgi?id=477.

Any pointers and suggestions on how to best create such a selection
list of Python objects is greatly appreciated!

Thanks!
Thomas
_______________________________________________
PySide mailing list
PyS...@lists.openbossa.org
http://lists.openbossa.org/listinfo/pyside

Hugo Parente Lima

unread,
Dec 15, 2010, 8:13:26 AM12/15/10
to pys...@lists.openbossa.org
On Saturday 20 November 2010 14:30:19 Thomas Perl wrote:
> Hi!
>
> I'm trying to display a list of arbitrary Python objects inside a QML
> ListView where I can leave the representation of each Python object to
> the delegate. Ideally (ignoring all wrapping in QObjects with slots,
> properties, etc..), I'd like to do something along those lines:
>
> class Person:
> def __init__(self, id, name):
> self.id = id
> self.name = name
>
> # Imagine this is properly wrapped as slot in a QObject
> def person_selected(p):
> print 'user clicked on person:', p.name, 'with id:', p.id
>
> persons = [
> Person(1, 'AA'),
> Person(2, 'BB'),
> Person(3, 'CC')
> ]
>
> # ... create QDeclarativeView ...
> # ... get root context as "ctx" ...
>
> ctx.setContextProperty('persons', persons)
> ctx.setContextProperty('person_selected', person_selected)

There are two signatures for setContextProperty:

setContextProperty(QObject*)
setContextProperty(QVariant)

When you pass anything that isn't a QObject PySide uses the QVariant overload,
so when you pass a list of Persons, PySide also uses the QVariant overload,
but this time it creates a QVariant of PyObjects because Qt doesn't know how
to handle PyObjects neither QML.

Try to inherit the Person class from QObject, a big overhead IMO, but should
work *if* QML understands a list of QObjects as a model otherwise you will
need to create a QListModel and find a way to access the model data roles from
QML, so your delegate can use them.

> In my QML file, I then want to set "persons" as the model for my
> ListView, and set up the mouse handler in the delegate to call
> person_selected() with the person that has been clicked. In the
> delegate, I want to access the attributes of Person (e.g. p.name), and
> (thinking about future uses), I might also use methods of it (e.g.
> p.formatBirthday()) as well, not just attributes.
>
> In the pyside-examples repository, I've only found a simple example
> that deals with a list of strings (in
> examples/declarative/scrolling.py), which works and is nice, but does
> not really map to my real-world use case where I want to display a
> list of items that have different properties and where I also want to
> retrieve the original item in a "item selected" callback.
>
> Trying to come up with something as described above does not give me
> any results. I have tried playing around with QAbstractListModel as
> alternative already, which resulted in
> http://bugs.openbossa.org/show_bug.cgi?id=477.
>
> Any pointers and suggestions on how to best create such a selection
> list of Python objects is greatly appreciated!
>
> Thanks!
> Thomas
> _______________________________________________
> PySide mailing list
> PyS...@lists.openbossa.org
> http://lists.openbossa.org/listinfo/pyside

--
Hugo Parente Lima
INdT - Instituto Nokia de Tecnologia

signature.asc

Hugo Parente Lima

unread,
Dec 15, 2010, 9:16:32 AM12/15/10
to pys...@lists.openbossa.org
On Saturday 20 November 2010 14:30:19 Thomas Perl wrote:
> Hi!

Hi, I did a example to show, test and learn how to export a QAbstractItemModel
do QML, including user defined roles and... it works! :-D

The example is attached, I'll upload it to git soon.

--

view.qml
usingmodel.py
signature.asc

Thomas Perl

unread,
Dec 15, 2010, 2:33:40 PM12/15/10
to Hugo Parente Lima, pys...@lists.openbossa.org
Hi Hugo,

2010/12/15 Hugo Parente Lima <hugo...@openbossa.org>:


> On Saturday 20 November 2010 14:30:19 Thomas Perl wrote:
> Hi, I did a example to show, test and learn how to export a QAbstractItemModel
> do QML, including user defined roles and... it works! :-D
>
> The example is attached, I'll upload it to git soon.

I've figured that out already, some examples of which are in the
PySide wiki (the original question was from November 20th ;). Thank
you for the example, though. It would be great to have it in
pyside-examples :)

Greetings,

Hugo Parente Lima

unread,
Dec 15, 2010, 3:38:43 PM12/15/10
to Thomas Perl, pys...@lists.openbossa.org
On Wednesday 15 December 2010 17:33:40 Thomas Perl wrote:
> Hi Hugo,
>
> 2010/12/15 Hugo Parente Lima <hugo...@openbossa.org>:
> > On Saturday 20 November 2010 14:30:19 Thomas Perl wrote:
> > Hi, I did a example to show, test and learn how to export a
> > QAbstractItemModel do QML, including user defined roles and... it works!
> > :-D
> >
> > The example is attached, I'll upload it to git soon.
>
> I've figured that out already, some examples of which are in the
> PySide wiki (the original question was from November 20th ;). Thank
> you for the example, though. It would be great to have it in
> pyside-examples :)

It's already there =].

> Greetings,
> Thomas

signature.asc
Reply all
Reply to author
Forward
0 new messages