[PySide] How to expose a python list object to QML

1,731 views
Skip to first unread message

Luca Donaggio

unread,
Feb 16, 2011, 10:25:00 AM2/16/11
to PyS...@lists.openbossa.org
I have a python list which I wanto to expose to QML along with other properties of a QObject.

From QML I can access all other "basic types" properties, but when I try to do anything with the python list, It doesn't work.
Let's say my QObject is exposed as "myObj" to QML and "myList" is the property which holds the python list, neither looping through its elements:

for (var i = 0;i<myObj.myList.length;i++) {}

nor accessing a random element directly:

Text {text: myObj.myList[1]}

are working (the latter causing the error "Unable to assign [undefined] to QString text").

How am I supposed to do this?

--
Luca Donaggio

Thomas Perl

unread,
Feb 16, 2011, 10:43:03 AM2/16/11
to Luca Donaggio, PyS...@lists.openbossa.org
Hi Luca,

2011/2/16 Luca Donaggio <dona...@gmail.com>:
> [...]


> From QML I can access all other "basic types" properties, but when I try to
> do anything with the python list, It doesn't work.
> Let's say my QObject is exposed as "myObj" to QML and "myList" is the
> property which holds the python list, neither looping through its elements:
>
> for (var i = 0;i<myObj.myList.length;i++) {}
>
> nor accessing a random element directly:
>
> Text {text: myObj.myList[1]}
>
> are working (the latter causing the error "Unable to assign [undefined] to
> QString text").
>
> How am I supposed to do this?

Which version of PySide are you using? Also, for me it works when
setting a Python list as context property directly; I haven't tried
with properties on QObjects, though.

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

Luca Donaggio

unread,
Feb 16, 2011, 10:58:45 AM2/16/11
to Thomas Perl, PyS...@lists.openbossa.org
On Wed, Feb 16, 2011 at 4:43 PM, Thomas Perl <th....@gmail.com> wrote:
Hi Luca,

2011/2/16 Luca Donaggio <dona...@gmail.com>:
> [...]
> From QML I can access all other "basic types" properties, but when I try to
> do anything with the python list, It doesn't work.
> Let's say my QObject is exposed as "myObj" to QML and "myList" is the
> property which holds the python list, neither looping through its elements:
>
> for (var i = 0;i<myObj.myList.length;i++) {}
>
> nor accessing a random element directly:
>
> Text {text: myObj.myList[1]}
>
> are working (the latter causing the error "Unable to assign [undefined] to
> QString text").
>
> How am I supposed to do this?

Which version of PySide are you using? Also, for me it works when
setting a Python list as context property directly; I haven't tried
with properties on QObjects, though.

Thomas

Sorry, I should have mentioned it in my first post!
I'm using PySide 1.0.0~beta5 for Maemo.
Unfortunately I can't set the list as a context property, as I need it from within a ListModel.
Actually I'm setting it with something like this inside the QObject subclass:

myList = Property(list, _myList, notify = changed)

Maybe the "list" type is not the correct one?

--
Luca Donaggio

Thomas Perl

unread,
Feb 16, 2011, 11:09:24 AM2/16/11
to Luca Donaggio, PyS...@lists.openbossa.org
Hi Luca,

2011/2/16 Luca Donaggio <dona...@gmail.com>:
> On Wed, Feb 16, 2011 at 4:43 PM, Thomas Perl <th....@gmail.com> wrote:
>> Which version of PySide are you using? Also, for me it works when
>> setting a Python list as context property directly; I haven't tried
>> with properties on QObjects, though.
>

> Sorry, I should have mentioned it in my first post!
> I'm using PySide 1.0.0~beta5 for Maemo.
> Unfortunately I can't set the list as a context property, as I need it from
> within a ListModel.
> Actually I'm setting it with something like this inside the QObject
> subclass:
>
> myList = Property(list, _myList, notify = changed)
>
> Maybe the "list" type is not the correct one?

I don't know about the internals, but I think "list" should be the
correct type. Not knowing what you want to do exactly with the list
and its items, maybe you could move the processing/iterating over the
list into Python land and just expose the result as a single value to
QML? Or if that doesn't work, another idea would be to subclass
QObject and give it slots that provide a list-like API (size(), get(),
...) - yep, that's ugly and might not be the fastest way of doing
things.

Apart from that, maybe opening a bug/feature request at
http://bugs.openbossa.org/ seems like a good idea (post the bug link
here after submitting), as this is something that developers would
expect to work, and if it can be supported from the technical point of
view (Shiboken?), it probably should be.

Thanks,

Hugo Parente Lima

unread,
Feb 16, 2011, 11:57:00 AM2/16/11
to pys...@lists.openbossa.org
On Wednesday 16 February 2011 14:09:24 Thomas Perl wrote:
> Hi Luca,
>
> 2011/2/16 Luca Donaggio <dona...@gmail.com>:
> > On Wed, Feb 16, 2011 at 4:43 PM, Thomas Perl <th....@gmail.com> wrote:
> >> Which version of PySide are you using? Also, for me it works when
> >> setting a Python list as context property directly; I haven't tried
> >> with properties on QObjects, though.
> >
> > Sorry, I should have mentioned it in my first post!
> > I'm using PySide 1.0.0~beta5 for Maemo.
> > Unfortunately I can't set the list as a context property, as I need it
> > from within a ListModel.
> > Actually I'm setting it with something like this inside the QObject
> > subclass:
> >
> > myList = Property(list, _myList, notify = changed)
> >
> > Maybe the "list" type is not the correct one?

Maybe this would help:

http://lists.openbossa.org/pipermail/pyside/2010-December/001585.html

--
Hugo Parente Lima
INdT - Instituto Nokia de Tecnologia

signature.asc

Luca Donaggio

unread,
Feb 17, 2011, 4:45:39 AM2/17/11
to Hugo Parente Lima, pys...@lists.openbossa.org
Thanks Hugo, it definetly helps!

But... I can't find any documentation about QtDeclarative.ListProperty, are there any example? The PieChart one is nice, but it doesn't really answer one question: I have a python list and its getter method (it would be a read-only property, so I don't need to append anything from QML), how do I use ListProperty to just let QML *read* my property?

--
Luca Donaggio

Aidan O'Kelly

unread,
Feb 17, 2011, 1:52:13 PM2/17/11
to pys...@lists.openbossa.org
This is a bug guys, http://bugs.openbossa.org/show_bug.cgi?id=629

That bug entry is a bit messy, but that's the bug its describing
(python list's cannot be used a QObject properties )

If you print out a list object, from QML, that has been set using
setContextProperty, you get something like this:
list is:[object Object],[object Object],[object Object]
However if you use that list object as a property on a QObject, and
print that out, you get:
thing.list is:QVariant(PySide::PyObjectWrapper)

Now I found this while trying to use QAbstractListModel as a property,
which suffers the same problem, however there is a workaround for
that, you simply specify the property as 'QObject' type and it all
works fine.

Aidan.

Aaron Richiger

unread,
Feb 17, 2011, 3:51:26 PM2/17/11
to pys...@srvrec006.openbossa.org
Hello everybody!

I just wrote you yesterday because I couldn't get PySide working from
the source package (which is needed to setup the mysql plugin manually)
on my Mac 10.6.6. I didn't get any answers, seems nobody has the same
problem or has a solution. I finally solved the problem and now
everything works great: MySql (incl. QMYSQL driver), qt and PySide. If
anybody has the same problem, let me know, I will send you the detailed
configure parameters.

Thanks anyway, have a nice evening!
Aaron

Luca Donaggio

unread,
Feb 18, 2011, 5:50:32 AM2/18/11
to Aidan O'Kelly, pys...@lists.openbossa.org
On Thu, Feb 17, 2011 at 7:52 PM, Aidan O'Kelly <aid...@gmail.com> wrote:
This is a bug guys, http://bugs.openbossa.org/show_bug.cgi?id=629

That bug entry is a bit messy, but that's the bug its describing
(python list's cannot be used a QObject properties )

If you print out a list object, from QML, that has been set using
setContextProperty, you get something like this:
list is:[object Object],[object Object],[object Object]
However if you use that list object as a property on a QObject, and
print that out, you get:
thing.list is:QVariant(PySide::PyObjectWrapper)

Now I found this while trying to use QAbstractListModel as a property,
which suffers the same problem, however there is a workaround for
that, you simply specify the property as 'QObject' type and it all
works fine.

Aidan.



Thanks Aidan to point me in the right direction: so it's a bug in the end, and, unfortunaltely, it's not planned for a pre-1.0 release fix :(
Anyway, your workaround doesn't work for me: specifying the property as a QObject lead to a segmentation fault (I tried with a simple python list of strings and with the same python list wrapped by a QAbstractListModel subclass - same result).

--
Luca Donaggio

Aidan O'Kelly

unread,
Feb 18, 2011, 6:34:02 AM2/18/11
to Luca Donaggio, pys...@lists.openbossa.org
I get the segfault if I try returning a python list as a QObject, but
the AbstractList does work for me:

http://pastebin.com/VMeg4isb

That's some test code that should work.

Good news is also, you can return python lists as a 'role' from one of
your AbstractList, and it does work, you can also return another
AbstractList as a role and that works too. (there's an example of that
in the test code)

Hugo Parente Lima

unread,
Feb 18, 2011, 10:21:41 AM2/18/11
to pys...@lists.openbossa.org

Could you file a bug report with this example that cause a segfault?

signature.asc

Aidan O'Kelly

unread,
Feb 18, 2011, 1:23:07 PM2/18/11
to Hugo Parente Lima, pys...@lists.openbossa.org
The bug doesn't actually cause a segfault. Its only the attempted
workaround that causes the segfault.

http://bugs.openbossa.org/show_bug.cgi?id=629

I've updated this with a much cleaner example of the bug. dict's are
affected too.

Reply all
Reply to author
Forward
0 new messages