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 > [email protected] > http://lists.openbossa.org/listinfo/pyside -- Hugo Parente Lima INdT - Instituto Nokia de Tecnologia
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ PySide mailing list [email protected] http://lists.openbossa.org/listinfo/pyside
