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
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to