Hi Jean,

I took a look on your code, and my first answer is YES, you need keep
the QObject reference. But you can replace the QAbstractListModel.data
function to return some copyable object to avoid keep the ref live.
You can use string, numbers to return the value based on the role
name.

Something like that:

    def data(self, index, role):

        if index.isValid():
            if role == ThingListModel.COLUMNS.index('thing'):

              return self._data['thing']
            if role == ThingListModel.COLUMNS.index('thing2'):
              return self._data['thing2']
            if role == ThingListModel.COLUMNS.index('thing3'):
              return self._data['thing3']
        return None


This will avoid you to keep a QObject live for each row in your list,
and you can mix this with some database (use a database on the
self.data object) to keep the objects in the disk while not requested.


This is my idea I think there are others ideas to make this fast and light.

BR









On Mon, Jan 10, 2011 at 6:26 AM, Jean-Baptiste Rudant
<[email protected]> wrote:
> Hi list,
>
> I read the very good example in the wiki
> (http://developer.qt.nokia.com/wiki/Selectable_list_of_Python_objects_in_QML)
> and the thread "Updating QML ListView content from PySide". It helped me a
> lot, but I still have a question.
> In these examples, the list of wrappers is generated once for all (things =
> [ThingWrapper(thing) for thing in people]). But in reality, I don't want to
> do that, because I have a large number of items which come from a database.
> So I tried to modify the example in order to call the wrapper only when the
> model returns data. But in my first tries, it didn't work, I think because
> the reference to the object was lost.
> My first bad idea was to keep reference with something like this (but it
> keeps all items in memory, which is what I want to avoid).
> # in realty, things won't be a list of items (because in this case, keeping
> a list of wrappers wouldn't cost too much) but an iterator for example
> #things = [ThingWrapper(thing) for thing in people] becomes
> things = people
> # in ThingListModel.__init__
> self._remember_me_list = []
> # in ThingListModel.data
> remember_me = ThingWrapper(self._things[index.row()])
> self._remember_me_list.append(remember_me)
> return remember me
> My second idea was to use a global variable to keep reference in the python
> world to the object (I read this trick in the "Updating QML ListView content
> from PySide" thread but to solve another problem) :
> # in ThingListModel.data
> global remember_me
> remember_me = ThingWrapper(self._things[index.row()])
> return remember_me
> I attached the example from the wiki with my modifications. Is it the good
> the approach, or do I miss something ?
> Thanks for the help,
> Jean-Baptiste Rudant
>
>
>
> _______________________________________________
> PySide mailing list
> [email protected]
> http://lists.openbossa.org/listinfo/pyside
>
>



-- 
Renato Araujo Oliveira Filho
Instituto Nokia de Tecnologia - INdT
Mobile: +55 (81) 8704-2144
_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to